SQL Query输出打印

时间:2015-03-13 07:54:16

标签: mysql sql oracle

我有一个问题......员工表格如下:

EmpID Name ManagerID
    1    a         2
    2    b         3
    3    c         4
    4    d         1

期望的结果:

EmpID Name ManagerID ManagerName
    1    a         2 b
    2    b         3 c
    3    c         4 d
    4    d         1 a

请帮我解释如何打印输出。 感谢

5 个答案:

答案 0 :(得分:1)

您需要自己加入,因为经理也是员工

SELECT  e1.EmpID, e1.Name, e2.Name as ManagerName 
from employee as e1, employee as e2
where  e1.ManagerID=e2.EmpID

答案 1 :(得分:1)

 SELECT t1.EmpID, t1.Name, t1.ManagerID, t2.Name as `Manager`
 FROM employee t1 JOIN employee t2 
      ON t1.ManagerID = t2.EmpID

答案 2 :(得分:1)

您可以在同一张桌子上使用left join。如果某个员工没有经理,那么执行inner join将不会显示他们。所以它将是

select t1.*, t2.name as manager_name
from table_name t1
left join table_name t2 on t1.empid = t2.managerid

答案 3 :(得分:-1)

您可以在两个表之间进行连接,例如:

SELECT e.id, e.name, m.id, m.name 
FROM employee e, manager m 
WHERE e.managerId=m.id;

答案 4 :(得分:-1)

select e.EmpID,  e.Name,  e.ManagerID,  m.Name 
from employee e
left join manager m
on e.ManagerID=m.ManagerID