select decode(b,20,c,''),decode(b,50,c,'') from t_1 where a=1;

解决方案 »

  1.   

    select decode(b,20,c,'') as c1,decode(b,50,c,'') as c2 from t_1 where a=1;
      

  2.   

    SQL> create table testa (a number,b number,c varchar2(10));Table created.SQL> insert into testa values (1,20,'20a');1 row created.SQL> insert into testa values (1,30,'30a');1 row created.SQL> insert into testa values (1,40,'40a');1 row created.SQL> commit;Commit complete.SQL> select * from testa;         A          B C
    ---------- ---------- --------------------
             1         20 20a
             1         30 30a
             1         40 40a
    SQL> select decode(b,20,c,'') as c1,decode(b,50,c,'') as c2 from testa where a=1
    ;C1                   C2
    -------------------- --------------------
    20a
      

  3.   

    如果按照
    select decode(b,20,c,'') as c1,decode(b,40,c,'') as c2 from testa where a=1
    检索出的是两行.我想达到这样的效果,
    除非两个条件都不满足.要不检索出的结果始终是一行.在一次的感谢!!
      

  4.   

    SQL> select decode(b,20,c,'') as c1,decode(b,40,c,'') as c2 from testa where a=
     and (b=20 or b=40);C1                   C2
    -------------------- --------------------
    20a
                         40aElapsed: 00:00:00.30
      

  5.   

    还是不行呀,a= and  这里好象有问题把.
    而且补充一下,用的是ORACLE8.05.是不是有关系.
      

  6.   

    zhaoyongzhu(zhaoyongzhu)从上面你的结果好象也能看出来.
    你执行的结果是两条.我想达到这样的效果,
    如果两个条件都不满足.就没有检索出任何结果.
    只要满足一个或两个.检索出的结果就始终是一行.
      

  7.   

    sorry:
    SQL> select decode(b,20,c,'') as c1,decode(b,40,c,'') as c2 from testa where a=1
     and (b=20 or b=40);C1                   C2
    -------------------- --------------------
    20a
                         40aElapsed: 00:00:00.30
      

  8.   

    如果按照 select t1.c as c1 , t2.c as c2 from testa t1, testa t2
    where t1.a = '1' and t1.b = '20' and t2.a = '1' and t2.b = '40'
    来检索.
    根据现在的记录.
    结果是:
    c1   c2
    20a  40a  输出结果是一条记录.如果改变了上面的一个检索条件: 比如:t2.b = '50'
    按照上面那句SQL应该是一条记录也没有检索出来.
    我想改成这样的效果:c1   c2
    20a  ""  c2虽然没有满足的记录仍然输出空.   输出结果也是一条记录.
      

  9.   

    zhaoyongzhu的就可以,输出的就是空串,等于null,你也可以''改成'no data'
      

  10.   

    我可能要得分了:select t1.c as c1,t2.c as c2 from testa t1,testa t2 where t1.b = 20 and t1.b + 20 = t2.b(+);c1 c2
    20a 40a
    select t1.c as c1,t2.c as c2 from testa t1,testa t2 where t1.b = 20 and t1.b + 30 = t2.b(+);c1 c2
    20a
      

  11.   

    SQL> select (select c from testa where a=1 and b=20) as c1 ,(select c from testa
     where a=1 and b=40) as c2 from dual;C1                   C2
    -------------------- --------------------
    20a                  40aElapsed: 00:00:00.50
    SQL> select (select c from testa where a=1 and b=20) as c1 ,(select c from testa
     where a=1 and b=50) as c2 from dual;C1                   C2
    -------------------- --------------------
    20aElapsed: 00:00:00.60
      

  12.   

    我用的是ORACLE8.05,不能支持子查询的.
    但还是要感谢你们.liuhenger(热情) 你少了一个检索条件.a
      

  13.   

    xiaobeibei(小贝贝) : 在本例中条件a是没有用的。实际的应用可以加上去。这种查询不是什么高级东东,oracle7.3都有的。
      

  14.   

    liuhenger(热情) (  ) 我知道.
    就当前的数据而言是没有用的,
    但实际的情况是a是一个检索条件.就是说,实际的数据a是一个变化的项目.
    需要加上检索条件.
      

  15.   

    xiaobeibei(小贝贝): 你的问题解决了吗?还有什么问题么?
      

  16.   

    liuhenger(热情)谢谢你的关心,
    如果加上a这个检索条件你给的部分就有问题.
    能不能把a也帮我加上去呢.
      

  17.   

    SQL> select t1.c as c1,t2.c as c2 from testa t1,testa t2 where (t1.a=1 or t2.a=1
    ) and t1.b = 20 and t1.b + 30 = t2.b(+);C1         C2
    ---------- ----------
    20aElapsed: 00:00:00.30
    SQL> select t1.c as c1,t2.c as c2 from testa t1,testa t2 where (t1.a=1 or t2.a=1
    ) and t1.b = 20 and t1.b + 20 = t2.b(+);C1         C2
    ---------- ----------
    20a        40aElapsed: 00:00:00.30
      

  18.   

    不是很明白你的应用,仅仅是加上条件a,就这么简单:
    select t1.c as c1,t2.c as c2 from testa t1,testa t2 where t1.a=1 and t1.b = 20 and t1.b + 30 = t2.b(+);c1 c2
    20a select t1.c as c1,t2.c as c2 from testa t1,testa t2 where t1.a=1 and t1.b = 20 and t1.b + 20 = t2.b(+);c1 c2
    20a 40a
      

  19.   

    liuhenger(热情)如果a中不仅仅是"1"这样的记录.
    你加一些a为"2"这样的记录进去的时候.在执行一下你的代码看看. zhaoyongzhu(zhaoyongzhu)如果你加一些a为"2"这样的记录在执行,结果就更加奇怪了.
      

  20.   

    select c c1 from t_1 where a='1' and '20'
     union 
    select ,c c2  from t_1 where a='1' and '50'
      

  21.   

    select DECODE( name,'20', dept) as c1,DECODE( name,'50',dept) as c2 from a 
    where id=1 and (name='20' or name='50') ;//select dept as c1 from a where id=1 and name='20'
    // union  
    //select DECODE(count(dept),null,'') as c2 from a where id=1 and name='50';
      

  22.   

    select DECODE( b,'20', c) as c1,DECODE( b,'50',c) as c2 from a 
    where a=1 and (b='20' or b='50') ;
      

  23.   

    select DECODE( b,'20', c) as c1,DECODE( b,'50',c) as c2 from t_1
    where a=1 and (b='20' or b='50') ;这样可以吧,跟zhaoyongzhu(zhaoyongzhu)的 很相象
      

  24.   

    方法虽然笨点,但绝对正确:
    select a.c1,b.c2 from (select '1' as bz ,c as c1 from testa where a = 1 and b = 20) a,
                          (select '1' as bz ,c as c2 from testa where a = 1 and b = 50) b
     where a.bz = b.bz(+) union
    select a.c1,b.c2 from (select '1' as bz ,c as c1 from testa where a = 1 and b = 20) a,
                          (select '1' as bz ,c as c2 from testa where a = 1 and b = 50) b
     where a.bz(+) = b.bz;
      

  25.   

    qiuyang_wang(小数点)又一次的佩服你了.
    其他的同志也感谢了.问题解决了.