2015-04-01 60 views
1

问题: 我已经仿照我的MySQL表来模拟表之间的继承,例如:确定子表的类型

Example model

是否有查询员工和输出哪种类型什么好办法他们是员工吗?

所需的输出将是:

id name        salary  type 
------------------------------------------------------------ 
1 John Doe       2000  executive 
2 Jane Doe       1000  manager 
3 Nick Carter      500  intern 

任何帮助表示赞赏!

+0

虽然可以使用在回答中提供的选择,我建议你改变你的设计,并添加列“employee_type”给员工。当多个或不是(实习生,经理,行政人员)有一个员工的行时,您当前的结构将会出现问题。 – Thaylon 2015-04-01 08:44:48

回答

0

你可以有一个表维护雇工的类型,但是在这种情况下,你可以做

select 
e.* , 
case 
    when i.employee_id is not null then 'intern' 
    when m.employee_id is not null then 'manager' 
    when ex.employee_id is not null then 'executive' 
end as `type` 
from employee e 
left join intern i on i.employee_id = e.id 
left join manager m on m.employee_id = e.id 
left join executive ex on ex.employee_id = e.id 
+0

谢谢,非常感谢! – DrDumbom 2015-04-01 08:44:03