一个sql select case when语句的例子

发布时间:2020-10-04编辑:脚本学堂
在sql数据库中使用select case when语句,可以实现多条件查询与结果输出,通过例子了解下select case when语句的具体用法。

select case when语句
 

复制代码 代码示例:
select  d.user_id as '工号',d.user_name as '姓名', c.diathesisconfig_year as 'year',
case a.diathesis_name when '应变能力' then  b.diathesis_detailcontent  else '' end as '应变能力',
case a.diathesis_name when '学习发展' then  b.diathesis_detailcontent  else '' end as '学习发展',
case a.diathesis_name when '活出玫琳凯' then  b.diathesis_detailcontent  else '' end as '活出玫琳凯',
case a.diathesis_name when '制定决策' then  b.diathesis_detailcontent  else '' end as '制定决策',
case a.diathesis_name when '激励激发' then  b.diathesis_detailcontent  else '' end as '激励激发',
case a.diathesis_name when '规划实现' then  b.diathesis_detailcontent  else '' end as '规划实现',
case a.diathesis_name when '策略思维' then  b.diathesis_detailcontent  else '' end as '策略思维',
case a.diathesis_name when '关注细节' then  b.diathesis_detailcontent  else '' end as '关注细节',
case a.diathesis_name when '树立信誉' then  b.diathesis_detailcontent  else '' end as '树立信誉',
case a.diathesis_name when '创造愿景' then  b.diathesis_detailcontent  else '' end as '创造愿景',
case a.diathesis_name when '沟通影响' then  b.diathesis_detailcontent  else '' end as '沟通影响',
case a.diathesis_name when '引领方向' then  b.diathesis_detailcontent  else '' end as '引领方向',
case a.diathesis_name when '团队合作' then  b.diathesis_detailcontent  else '' end as '团队合作',
case a.diathesis_name when '创新思维' then  b.diathesis_detailcontent  else '' end as '创新思维',
case a.diathesis_name when '自我激励' then  b.diathesis_detailcontent  else '' end as '自我激励',
case a.diathesis_name when '利用资讯' then  b.diathesis_detailcontent  else '' end as '利用资讯'
from mlk_pdp.dbo.test11 a,mlk_pdp.dbo.test22 b,mlk_pdp.dbo.test33 c,mlk_pdp.dbo.sec_user d
where c.user_id=d.user_id
and (b.diathesis_detailid=c.diathesis_detailid 
and a.diathesis_sysid=b.diathesis_sysid  or a.parent_id=b.diathesis_sysid)

以上sql语句中,集中展示了case when else end语句组合的用法。