平常不怎么接触sql,关于case when一直很模糊,想问下,如果我有这么个情况,就是说,如果a=2或者b=3的情况下 c=4,如果a=5的情况下,c=5,请问用case when 怎么写呢?求教

解决方案 »

  1.   

    declare
     a number := 2;
     b number := 3;
     c number;
    begin
     select (case when a=2 or b = 3 then 4 when a = 5 then  5 end) into c from dual;
     dbms_output.put_line(c);
    end;
      

  2.   

    case when a=2 or b=3 then c=4 when a=5 then c=5 end; --like this ???
      

  3.   


    case when 条件1 then 语句1或表达式1
         when 条件2 then 语句2或表达式2
         ......
    else 语句n或表达式n
    end
      

  4.   

    case when a=2 or b=3 then c=4
         when a=5 then c=5 end
      

  5.   

    declare
    a int :=2;
    b int :=4;
    c int ;
    begin
    c :=case when a=2 or b=3 then 4 when a=5 then 5 end ;
    dbms_output.put_line('c的值:'||c);
    end;
      

  6.   

    case 有两种用法.
    a=1则取2否则取3
    case a when 1 then 2 else 3 end 
    case when a=1 then 2 else 3 end
      

  7.   

    --还可以加上else 
    case when (a=2 or b=3) then c=4
      when a=5 then c=5 
    else c=10 end