2012-03-30 207 views
-1

可能重複:
Employee Manager Report - Sql Server關於SQL查詢?

我有一個Employee表是這樣的:

Eno Ename Mangerid 
1 X  Null 
2 Y  1 
3 Z  2 

但我想結果如下圖所示

Ename Managername 
X  Null 
Y  X 
Z  y 

任何人都可以給我這個答案嗎?

在此先感謝

+0

你可能感興趣的東西是嵌套集。喬Celko有一些非常好的文章。請參閱:(樹中的SQL)http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html – 2012-03-30 12:40:34

回答

2

這應該工作:

select 
    e.Ename, 
    m.Ename as ManagerName 
from employee e 
left join employee m on e.ManagerId = m.Eno 
+0

謝謝,它的工作 – mani654 2012-04-05 09:28:14

0
select e1.Ename employee, e2.Ename manager 
from employee e1 
join employee e2 on e1.ManagerId = e2.Eno 
+0

謝謝,但它給出了結果YX和ZY – mani654 2012-04-05 09:29:44

0

使用自助加入對員工表

SELECT EMPLOYEE.ENAME as EmployeeName, EMPLOYEE.ENO as ManagerName 
FROM EMPLOYEE 
INNER JOIN EMPLOYEE AS EMPLOYEE_1 
ON EMPLOYEE.ENO = EMPLOYEE_1.MANEGERID 

+0

謝謝,但它給出了結果YX和ZY – mani654 2012-04-05 09:30:37

0
Select e.Name, (select m.name from Employee M where M.Eno =e.Mangerid) as Manager 
from Employee e