简体   繁体   中英

Getting a value using only the primary key and foreign key

I have already tried several queries and joins and I can't solve this question.

I have 2 tables

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 table consists of 2 data, the first one is.

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

2nd one is

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

3rd one is for the teamleader table

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

I've tried this query

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

but the result is the teamleader id was only shown. Is it possible to select all the employee data including the name of the teamleader ?

You need another join for getting result:

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';

I added left join for employeee without teamleader.

Please try the query as shown. The issue with your posted query is that you are not selecting the team leader's name, only the original employee. To get the team leader's name as well, you will have to join to another instance of the employee table (employee_1) and link to the employeeId field in table teamleader.

SELECT e.*, m.FirstName as TeamLead
FROM employee e
join teamlead on e.TeamleaderId = teamlead.ID
join employee m on teamlead.employeeID = m.ID
WHERE e.FirstName = "Rinnie";

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM