文章目录
- 1.case when
- 2.if else
1.case when
完整的语法为:case … when … then … else … end
case 字段或表达式(或者不写)
when case字段的值,或者case表达式的值
then 满足条件执行的操作
else 不满足条件执行的操作
end 结束(不能缺少)
例子
表:
员工表,mgr:直属领导,hiredate:入职时间,sal:月薪
job:工作岗位 , comm :奖金
-- case when的基本使用
1.查询出员工所有信息并查询出该员工的工作是否是经理
select *,
case job when 'manager' then '经理'
else '员工'
end
from emp;
2.查询员工的年薪
select *,
if(comm is null,sal * 12,sal*12+comm)
as yrsal
from emp order by yrsal desc;
2.if else
语法为
if(判断表达式, true, false)
例子:
还是使用上面的表
查询员工的年薪
-- if else:if(条件,真,假),类似于java的三目运算符
select *,
if(comm is null,sal * 12,sal*12+comm)
as yrsal
from emp order by yrsal desc;