if 条件1 成立 then 执行 select 1 from a elseif if 条件2 成立 select 1 from b else select 3 from c end if;以上 如此的语句,用sql文怎么实现呢?

解决方案 »

  1.   

    select (case when 条件1 then 执行 when 条件2 then 执行2 else 执行其他 end) from 表
      

  2.   

    select 1 from a where 条件1
    union all
    select 1 from b where !(条件1) and 条件2
    union all
    select 1 from c where !(条件1) and !(条件2) and 条件3
      

  3.   

      使用动态SQL:select 1 from || ' table_name '; 不行吗? 
      

  4.   

    select 1 from a
    where 条件1
    union all 
    select 1 from b
    where 条件2
    union all 
    select 3 from c
    where 条件3
      

  5.   


    SQL> ed
    已写入 file afiedt.buf  1  select decode(&express,'1=1',(select count(1) from dual where 1=1),
      2* '2<>3',(select count(1) from dual where 2<>3)) from dual
    SQL> /
    输入 express 的值:  '1=1'
    原值    1: select decode(&express,'1=1',(select count(1) from dual where 1=1),
    新值    1: select decode('1=1','1=1',(select count(1) from dual where 1=1),DECODE('1=1','1=1',(SELECTCOUNT(1)FROMDUALWHERE1=1),'2<>3',(SELECTCOUNT(1)FROMDU
    --------------------------------------------------------------------------------
                                                                                   1
      

  6.   

    select 1 from a
    where 条件1
    union all  
    select 2 from b
    where 条件2
    union all  
    select 3 from c
    where 条件3
      

  7.   

    1、不能从case、decode函中返回融整个数据集,针对单行是可以的;
    2、如果条件1和条件2可以放在select中的where子句中,使用一个sql是可以的。
      

  8.   

    mssql,这样应该可以的,oracle 不行?
      

  9.   

    用union all 连接三个查询是可以的,祈祷你的三张表类型一样~~
      

  10.   

    select (case when 条件1 then 执行 when 条件2 then 执行2 else 执行其他 end) from 表ORACLE 这样写也是正确的··  而且最容易识别和思考··
      

  11.   

    但是then的执行语句,如果返回多记录就不支持了!