2016-12-07 41 views
-1

我必須解決的一個問題如何獲取上司的名字

我有兩個表,如員工和主管

Employees table 
------------------------    
firstname  
lastname  
employeeid 


Supervisors table 
---------------    
supervisor(employeeid)  
employeeid (employeeid)  
and other columns, 

困惑:

由此我不得不從取監事名稱從Supervisors表中提取主管的員工表。

在此先感謝

回答

1
SELECT FirstName, LastName 
FROM Employees 
WHERE Employeeid IN (
    SELECT Distinct supervisor FROM Supervisors 
) 

OR

SELECT DISTINCT a.FirstName, a.LastName 
FROM Employees a 
INNER JOIN Supervisors b ON a.EmployeeID = b.supervisor 

更新:

SELECT b.FirstName, b.LastName, c.FirstName, c.LastName 
FROM Supervisors a 
LEFT JOIN Employees b ON a.EmployeeID = b.supervisor 
LEFT JOIN Employees c ON a.EmployeeID = c.employeeid 
+0

我neeed employeename從你的問題的主管名字 – yuva

+0

你只想要supervisorname一起。但無論如何我更新了我的答案 – anon