case 语句的使用,case -> when -> then
相当于 Switch -> if -> 结果
举个例子,如果 salary 的值小于 1000,加 100 元
如果 salary 的值大于1000 小于 2000,加 200 元
其他情况加 300 元:
结果是 subSalary
case when salary < 1000 then salary += 100
when salary > 1000 && salary < 2000 then salary += 200
else salary += 300
end subSalary
本题的结果:连表查询加 case 语句
select e.emp_no, e.first_name, e.last_name, eb.btype, s.salary,
case when eb.btype = 1 then s.salary * 0.1
when eb.btype = 2 then s.salary * 0.2
else s.salary * 0.3
end bonus
FROM emp_bonus AS eb, employees AS e, salaries AS s
WHERE eb.emp_no = e.emp_no
AND s.emp_no = e.emp_no
AND s.to_date='9999-01-01';