0

I have a table which looks like this: Employee table:

Employee_id  E_Name  Manager_Id 
-------------------------------
1             p          -
2             q          1
3             r          1
4             s          2

Here column Manager_Id denotes the manager's employee Id. Meaning for p there is no manager. for q and r employee with id 1 is manager (p). and for s employee with emp id 2 is manager.

I want to build a query which will return below result:

Employee_id  E_Name  Manager_Name 
-------------------------------
1             p          -
2             q          p
3             r          p
4             s          q

table here has manager name instead of id.

How to achieve above? Thanks in advance

4

3 回答 3

1

可以使用 LEFT OUTER JOIN 轻松完成:

尝试这个:

SELECT a.EMPLOYEE_ID, a.E_NAME, b.E_NAME
FROM EMPLOYEE a left outer join EMPLOYEE b
on A.MANAGER_ID = B.EMPLOYEE_ID
于 2013-04-08T10:08:38.377 回答
0

您没有提到数据库提供程序,但对于MySQL,它是:

SELECT e.employee_id, e.e_name,COALESCE(m.e_name,'-') AS Manager_Name
FROM employee e 
LEFT JOIN employee m ON m.employee_id = e.manager_id 
于 2013-04-08T10:01:25.273 回答
0

请试试:

select 
    Employee_id, 
    E_Name, 
    (select E_Name from YourTable b where b.Employee_id=a.Manager_Id) as Manager_Name
from YourTable a
于 2013-04-08T10:03:57.017 回答