比如一条记录有c1,a1,a2,a3,b1,b2,b3 字段如果c1='a',则输出a1,a2,a3
如果c1='b',则输出b1,b2,b3使用 case when c1='a' then a1 else b1 end 的话只能输出1个字段
有什么更好的办法输出全部字段??

解决方案 »

  1.   

    没太看明白你的内容。你 case when c1='a' then a1 else b1 end ,你自己都只是对a1,b1进行了操作,当然只输出1个字段了。为什么不是:case when c1='a' then a1,a2,a3 else b1,b2,b3 end  呢?
      
      

  2.   

    @ youlostme 我试过你这个会报错的,无法同时输出a1,a2,a3 会提示a1 ‘,’附近有错误
      

  3.   

    case when c1='a' 
      then rtrim(a1)+','+rtrim(a2)+','+rtrim(a3) 
      else rtrim(b1)+','+rtrim(b2)+','+rtrim(b3)
    end 返回的字段,自己再解析一下
      

  4.   


    肯定会报错的,因为语句不完整。你用的ado读取数据库吗?
      

  5.   

    @ songtreer按你这样,不是把a1,a2,a3整合只输出了1个字段嘛?
    我需要完整的原始字段输出
      

  6.   

    @ youlostme case when then语句 只能针对1个字段的,是你的错误。。
      

  7.   

    我一般这样写,(笨方法,呵呵):adoqouery1.sql.clear;
    adoquery1.sql.add('select * from 你的表名');
    adoquery1.open;while not adoquery1.eof do
    begin
      if adoquery1.fielfvalues['c1']=a then 
      begin //里面写成你想要的语句
        showmessage(adoquery1.fielfvalues['a1']);
        showmessage(adoquery1.fielfvalues['a2']);
        showmessage(adoquery1.fielfvalues['a3']);
      end
      else begin  //里面写成你想要的语句    
        showmessage(adoquery1.fielfvalues['b1']);
        showmessage(adoquery1.fielfvalues['b2']);
        showmessage(adoquery1.fielfvalues['b3']);
      end;
      adoquery1.next;
    end;
    不知道我这样理解你的问题,对不对,也没准我把你的问题理解错了。呵呵
      

  8.   

    上面打错了字母,应该是:adoquery1.fieldvalues
      

  9.   


    是啊,我说了语句不完整吗。我把你说的a1理解成一条语句了。多条语句执行,要加 begin..end呀。
      

  10.   

    lz要不把三个字用特殊字符连接拼成一个字段,最后在程序中解析,要不就在sql里写三个case,要不就想 youlostme 写的在程序中自己用if判断。
      

  11.   

    @youlostme 可能是你理解错了我的问题我目前的解决方法如下:
    select case when c1='a' then a1 else b1 end,
    case when c1='a' then a2 else b2 end,
    case when c1='a' then a3 else b3 end
    from table但是觉得字段多了就很累赘,求简便方法
      

  12.   

    SQL語句寫成如下可以解決
    select a1 as D1,a2 as D2,a3 as D3
    from TabelA 
    where c1='a'
    union
    select b1 as D1,b2 as D2,b3 as D3
    from TabelA
    where c1='b'
      

  13.   

    @ Oraclers不错,要的就是这个,一开始就想用union的,呵呵。
      

  14.   

    这样呢?
    case when c1='a' then
    begin
      a1;
      a2;
      a3;
    end 
    else begin
      b1;
      b2;
      b3; 
    end;