语句:case when then else end
这语句,说简单不简单,说不简单也简单。啧啧
case:简单函数and搜索函数俩种
case简单函数
case sex when '0' then '男' when '1' then '女' else '做手术?' end
case搜索函数
case when sex = '0' then '男' when sex = '1' then '女' else '做手术' end
类似if-else语句
例子1:
case
when '喜欢我吗' in ( '喜欢', '不喜欢') then '不知道'
when '喜欢我吗' in ('喜欢') then '她喜欢我'
else'不喜欢我'end
上述例子中,第三行中的结果会被第二行给拦截下来,结果永远是不知道
你想想多可怕,人家喜欢你你却一直不知道,你不单身谁单身!(敲黑板!!)
例子二:计算喜欢你的人按身高计算数量(想想就激动)
select
case when height <= 1.5 then '1'
when height > 1.5 and height <= 1.6 then '2'
when height > 1.6 and height <= 1.7 then '3'
else '4' end ,
count(*)
from love_table
group by
case when height <= 1.5 then '1'
when height > 1.5 and height <= 1.6 then '2'
when height > 1.6 and height <= 1.7 then '3'
else '4' end
这样就会得到结果啦
例子三:cheak约束中试用case函数
女朋友工资必须高于6k
constraint check_salary check(
case when sex = '1'
then case when salary > 6000 then 1 else 0 end
else 0 end = 1 )
假如单纯使用check,如下所示
constraint check_salary check ( sex = '1' and salary > 6000 )
男性收入的情况将无法填入