if语句
if(判断,true,flase);
case语句 使用一
case 要判断的字段和表达式
when 常量1 then 要显示的值或语句(语句加;)
when 常量2 then 要显示的值或语句(语句加;)
...
else 要显示的值n或语句n 默认;
end
#查询员工的工资,要求
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
select salary 原始工资,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资
from employees;
case 使用二 类似if
case
when 条件1 then要显示的值或语句;
when 条件2 then要显示的值或语句;
else 默认的值
end
##查询员工的工资情况
工资大于20000,显示A级别
工资大于15000,显示B级别
工资大于10000,显示C级别
否则,显示D级别
select salary,
case
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
end as 工资级别
from employees;