1.首先创建两个表emp,emp_bonus如下:
(1)emp_bonus:
(2)emp:
2.首先对emp_bonus表进行操作:
select emp_bonus.*,(case when empno=7934 then 0
when empno=7839 then 1 else -1
end) as asd from emp_bonus
效果:
可见case when then 的作用或者效果是根据条件重新添加了一列!其实这一列可以的多表关联中进行其他有效的操作。
3.如,员工的奖金根据emp_bonus中的type来计算,type=1则工资的10%作为奖金,type=2则工资的20%作为奖金,以此类推……现在要求返回10号部门的员工工资及奖金:
select e.deptno,e.deptno,e.ename,e.sal,
(e.sal*case
when eb.type=1 then 0.1
when eb.type=2 then 0.2
when eb.type=3 then 0.3 end) as bonus from emp e inner join emp_bonus eb on(e.empno=eb.empno) and e.deptno=10
可见,两表关联后的bonus列进行了操作……