我想用一个查询语句实现如下功能:
从A表中查询B这个字段,如果B这个字段是NULL就查询C这个字段.
想用Select case来做但是不知道Orc中是怎么写的,麻烦帮忙给个参照.3Q

解决方案 »

  1.   


    SQL> select * from test.t;                                    IDX CNAME
    --------------------------------------- ------------------------------------------------------------
                                          1 aaaa
                                          2 bbbb
                                          3 ccccSQL> select decode(idx,1,cname),cname from test.t;DECODE(IDX,1,CNAME)                                          CNAME
    ------------------------------------------------------------ ------------------------------------------------------------
    aaaa                                                         aaaa
                                                                 bbbb
                                                                 ccccSQL> select decode(idx,1,cname,88888),cname from test.t;DECODE(IDX,1,CNAME,88888)                                    CNAME
    ------------------------------------------------------------ ------------------------------------------------------------
    aaaa                                                         aaaa
    88888                                                        bbbb
    88888                                                        cccc
      

  2.   

    select case when B is null then C else B end FName from A;
      

  3.   

    select decode(b,null,c,b) from a
      

  4.   

    case结构:case  xxxx
       when ... then 
           ... 
       when ... then
           ... 
       else 
           ... 
    end或case 
       when ... then 
           ... 
       when ... then
           ... 
       else 
           ... 
    end
    建议用这个,Oracle、DB2 通用
      

  5.   

    select decode(b,null,c,b) from a
    用这个要好一些,
    select case when B is null then C else B end FName from A;
    用这个的时候似乎要两个字段类型要相同
      

  6.   

    select   case  when T_SIMEINO is null then SIMEINO  else T_SIMEINO end as AAA from TB_MOSIKOMI
    我也自己解决了,谢谢二楼
    分少只能意思意思了.3Q
      

  7.   

    sql server 用法:
    select case when b is null then c else b end from 表oracle 用法更简单:
    select decode(b,null,c,b) from 表两个语句的较果是一样的;