1

表员工有 ssn 和 superssn 列。我想显示每个员工的主管的主管(如果不存在,则为 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

4

1 回答 1

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;
于 2013-01-22T17:15:08.450 回答