2012-01-28 50 views

回答

0

你可以做一個自聯接的一部分,連接的第一個表的ManagerId (員工)EmpId第二次使用員工(經理):

SELECT e.empid, e.empname, e.salary 
    , COALESCE(m.empname, 'NoManager') AS ManagerName 
FROM Employee AS e 
LEFT JOIN Employee AS m ON e.ManagerId = m.empId 
+0

單獨的「內部加入」不起作用,因爲如果員工沒有經理,該員工將不會顯示。 – NoChance 2012-01-28 03:07:20

+0

@EmmadKareem好點,謝謝。我已更新爲使用'LEFT JOIN'。 – 2012-01-28 03:08:59

+0

謝謝你..它真的有幫助 – 2012-01-28 03:27:56

1
SELECT 
x.empid, 
x.empname as 'EmployeeName', 
x.salary as 'Salary', 
(SELECT empname FROM Tablename WHERE empid = x.managerid) as 'ManagerName' 
FROM 
Tablename x 
+0

謝謝你..它真的有助於 – 2012-01-28 03:28:13

+1

,它值得被標記爲答案。不是嗎? – Mubarek 2012-01-28 05:59:49

相關問題