表 mytable 内有两个字段
col1,col2我希望能够检索:如果某行的 col1 里无数据,则使用 col2 的数据。这条SQL该如何编写?
select ??? from mytable

解决方案 »

  1.   

    select case col1 when null then col2 end 
     from mytable
      

  2.   

    select case 
           when col1 is null then col2
           else col1
           end
    from mytalbe
      

  3.   

    如果你是字符型字段,还要考虑空字符串——''的情况
    select 
    case col1 
    when null then col2 
    when '' then col2 
    end 
    from mytable
      

  4.   

    select case when col1 is null then col2 else col1 end as col1 from mytable
      

  5.   

    --如果只考虑到null情况  用下面的就可以了select isnull(col1,col2) from mytable
      

  6.   

    哈虽然结帐了也发一下可以用 COALESCE这个更加方便