一张表“ORDERS” 字段 a b c d e f g h如果 d字段值不为空 则查询结果显示 a d e f g h 否则 显示a b c f g h该怎么弄? 

解决方案 »

  1.   

    这个需求有问题吧?!如果表里只有一行数据的话可以做得到
    if exists(select 1 from ORDERS where d is null)
      select a,b,c,f,g,h from ORDERS
    else
      select a,d,e,f,g,h from ORDERS如果有多行的话楼主想怎样显示数据?
      

  2.   

    a   d   e   f   g   h   否则   显示a   b   c   f   g   hSELECT a,CASE d IS NULL THEN b ELSE d END AS Column1,
    CASE d IS NULL THEN c ELSE e END AS Column2,
    f,g,h
    FROM ORDERS
      

  3.   

    select
    [col1]=a,
    [col2]=case when  d is not null then  d else b end,
    [col3]=case when  d is not null then  e else c end,
    [col4]=f,
    [col5]=g,
    [col6]=h
    from 
    t
      

  4.   


      select a,case when d is null then b else d end,case when d is null then c else e end,f,g,h from orders