Case when then (sql)

it2022-06-27  99

 

Case具有两种格式。分支结构case语句和条件判断case语句。

 

分支结构case语句,类似于switch

select ( case user_name  when  '张三'  then 'zhangsan' when '李四' then 'lisi' when '王五1' then 'wangwu'  else '找不到对应' end) as afrom ctl_user

 

条件判断case,类似于if else

select ( case  when  user_code > 0 and user_code < 100  then '一百以内' when user_code > 100 and user_code < 1000 then '一千以内' when user_code>1000 and user_code < 2000 then '一万以内'  else '不在范围' end) as afrom ctl_user

 

注意两者的写法。

还有一个需要注意的问题,分支结构case语句只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。

 

转载于:https://www.cnblogs.com/yoolonet/archive/2010/01/14/1647889.html


最新回复(0)