分享

case when 方法总结

 沐浴中的眼泪 2011-01-10
1、
case <单值表达式>
       when <表达式值> then <SQL语句或者返回值>
       when <表达式值> then <SQL语句或者返回值>
       ...
       when <表达式值> then <SQL语句或者返回值>
end
例如:
select (case t.hyml_dm
         when '01' then 'A'
         when '02' then 'B'
         when '03' then 'C'
         when '04' then 'D'
end) as hydmbm 
 from A
注意:这语句用在查询中,WHEN子句后面千万不要加逗号或者引号。否则就错了。
2、
case when <关系条件表达式> then <SQL语句或者返回值> end
例如:
select count(case when a.zt='07' then a.bs end),
    count(case when a.zt='11' then a.bs end)
  from dj_zt a
 where a.qylx_dm = '03'
3、
如果该字段为NULL值,想判断如果是NULL值,就转换成其它字段,如果不是NULL值,就显示原来字段
    select case when shjl is null then '99' else shjl end as shjlg from oa_xmmx

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多