case when 语法
case 字段
when 条件1 then 值1
when 条件2 then 值2
when 条件3 then 值3
else ‘其他’ end
或者
case
when 条件1 then 值1
when 条件2 then 值2
when 条件3 then 值3
else ‘其他’ end
case具有两种格式
简单case函数和case搜索函数
1.简单case函数,case后面有字段,例子:
Case sex
when 1 then ‘男’
When 2 then ‘女’
Else ‘未知’ end as sex,
2. case搜索函数,两个例子
demo1:
Case
when Sex = 1 then ‘男’
When sex= 2 then ‘女’
Else ‘未知’ end as sex,
demo2:
Select
sum(case when salary <5000 then 1 else 0 end) as salary_low,
Sum(case when salary >=5000 then 1 else 0 end) as salary_high
From person