仅使用主键和外键获取值

时间:2015-08-15 19:16:45

标签: mysql database

我已经尝试了多个queriesjoins,我无法解决这个问题。

我有2张桌子

Employee
--------
ID(PK)                                                                                                                                     
Firstname                                                                                                                                                  
Lastname                                                                                                                                                  
ContactNumber                                                                                                                                                  
Position                                                                         
TeamleaderID(FK) //*which is only applicable if the position isn't a team leader*//

Team Leader
----------
ID(PK)                                                                         
employeeID(FK)  

Employee表由2个数据组成,第一个是。

ID(PK) - 1                                                                         
Firstname - Mikael                                                                                                                               
Lastname - Roque                                                                                                                     
ContactNumber - 0010101                                                                                                             
Position - TeamLeader                                                                                                                                                                                              
TeamleaderID(FK) - Null 

第二个是

ID(PK) - 2                                                                         
Firstname - Rinnie                                                                         
Lastname - Hoshino                                                                         
ContactNumber - 0010101                                                                         
Position - Engineer                                                                         
TeamleaderID(FK) - 1

第三个用于teamleader

ID(PK) - 1                                                                         
employeeID(FK) - 1   

我试过这个查询

SELECT employee.*                                                                         
FROM employee                                                                         
JOIN teamleader                                                                         
ON employee.teamleader_id=teamleader.teamleader_id                                                                         
Where employee.firstname='Rinnie';  

但结果是只显示了teamleader id。是否可以选择所有employee数据,包括teamleader

的名称

1 个答案:

答案 0 :(得分:0)

您需要另一个联接才能获得结果:

SELECT employee.*,leader.*
FROM employee
LEFT JOIN teamleader ON employee.teamleader_id=teamleader.teamleader_id
LEFT JOIN employee as leader ON teamleader.employeeID = leader.ID
Where employee.firstname='Rinnie';

我为没有团队领导的员工添加了左连接。