表员工有ssn和superssn coloumns。我想显示每个员工的主管的主管(如果不存在,则为null)。到目前为止,我只有每位员工的主管:
SELECT p.fname AS sup,
group_concat(c.fname) AS emp
FROM employee AS p
LEFT JOIN employee AS c
ON (p.ssn = c.superssn)
GROUP BY p.ssn;
我应该再做一次JOIN
吗?或GROUP BY
中的某些内容?
答案 0 :(得分:1)
如果您想要经理,员工和经理的主管,那么您可以多次加入表employee
:
SELECT p.fname AS sup, -- Manager
group_concat(c.fname) AS emp, -- Employees
sp.fname as SuperSuper -- Manager Supervisor
FROM employee AS p
LEFT JOIN employee AS c
ON p.ssn = c.superssn
LEFT JOIN employee sp
on p.superssn = sp.ssn
GROUP BY p.ssn;