MySQL---case-when

MySQL—case-when

当Mysql查询语句需要某一个字段需要多条判断时,可以使用 case-when-then结构

就拿实例来看,某客网的Mysql题目—59

给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。 bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date=‘9999-01-01’

select e.emp_no,e.first_name,e.last_name,b.btype,s.salary,
(case b.btype 
 when 1 then s.salary*0.1
 when 2 then s.salary*0.2
 when 3 then s.salary*0.3
end) bonus
from employees e
join emp_bonus b on e.emp_no = b.emp_no
join salaries s on e.emp_no=s.emp_no
where s.to_date = "9999-01-01"
group by e.emp_no

猜你喜欢

转载自blog.csdn.net/qq_43288259/article/details/114403060
今日推荐