题目
员工表:Employees
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
+---------------+---------+
employee_id 是这个表的主键。
这个表中每一行中,employee_id 表示职工的 ID,employee_name 表示职工的名字,manager_id 表示该职工汇报工作的直线经理。
这个公司 CEO 是 employee_id = 1 的人。
用 SQL 查询出所有直接或间接向公司 CEO 汇报工作的职工的 employee_id 。
由于公司规模较小,经理之间的间接关系不超过 3 个经理。
可以以任何顺序返回的结果,不需要去重。
查询结果示例如下:
Employees table:
+-------------+---------------+------------+
| employee_id | employee_name | manager_id |
+-------------+---------------+------------+
| 1 | Boss | 1 |
| 3 | Alice | 3 |
| 2 | Bob | 1 |
| 4 | Daniel | 2 |
| 7 | Luis | 4 |
| 8 | Jhon | 3 |
| 9 | Angela | 8 |
| 77 | Robert | 1 |
+-------------+---------------+------------+
Result table:
+-------------+
| employee_id |
+-------------+
| 2 |
| 77 |
| 4 |
| 7 |
+-------------+
公司 CEO 的 employee_id 是 1.
employee_id 是 2 和 77 的职员直接汇报给公司 CEO。
employee_id 是 4 的职员间接汇报给公司 CEO 4 --> 2 --> 1 。
employee_id 是 7 的职员间接汇报给公司 CEO 7 --> 4 --> 2 --> 1 。
employee_id 是 3, 8 ,9 的职员不会直接或间接的汇报给公司 CEO。
生成数据
CREATE TABLE Employees7 (
employee_id INT,
employee_name VARCHAR (20),
manager_id INT
) ;
INSERT INTO Employees7 VALUE(1, 'Boss', 1),(3, 'Alice', 3),
(2, 'Bob', 1),(4, 'Daniel', 2),
(7, 'Luis', 4),(8, 'Jhon', 3),
(9, 'Angela', 8),(77, 'Robert', 1);
解答
查出直接向ceo汇报的id
SELECT E.`employee_id`
FROM Employees7 AS E
WHERE E.`manager_id` = 1;
这里其实需要去掉1的
再查出一次间接向ceo汇报的id
SELECT EE.`employee_id`
FROM Employees7 AS EE
WHERE EE.`manager_id` IN (SELECT E.`employee_id`
FROM Employees7 AS E
WHERE E.`manager_id` = 1 AND E.`employee_id` <> 1);
用左连接是可行的
逻辑是这样的 左连接一次 E1左连接E2 E2.manager_id为1 E1表的employee_id不为1
SELECT *
FROM Employees7 AS E
LEFT JOIN Employees7 AS EE
ON E.`manager_id` = EE.`employee_id`
WHERE EE.`manager_id` = 1 AND E.`employee_id` <> 1;
这个其实把 直接汇报和一次间接的都选出来了(由于CEO自己的manager_id也是1)
那三表连接取最左边就可以得到最后的结果了
SELECT E.`employee_id`
FROM Employees7 AS E
LEFT JOIN Employees7 AS EE
ON E.`manager_id` = EE.`employee_id`
LEFT JOIN Employees7 AS EEE
ON EE.`manager_id` = EEE.`employee_id`
WHERE EEE.`manager_id` = 1 AND E.`employee_id` <> 1;
即为结果
另外一种方法就是再查出二次间接汇报的 把直接 一次间接 二次间接的三者union
SELECT E.`employee_id`
FROM Employees7 AS E
WHERE E.`manager_id` = 1 AND E.`employee_id` <> 1
UNION
SELECT EE.`employee_id`
FROM Employees7 AS EE
WHERE EE.`manager_id` IN (SELECT E.`employee_id`
FROM Employees7 AS E
WHERE E.`manager_id` = 1 AND E.`employee_id` <> 1)
UNION
SELECT EEE.`employee_id`
FROM Employees7 AS EEE
WHERE EEE.`manager_id` IN (SELECT EE.`employee_id`
FROM Employees7 AS EE
WHERE EE.`manager_id` IN (SELECT E.`employee_id`
FROM Employees7 AS E
WHERE E.`manager_id` = 1 AND E.`employee_id` <> 1))
类似的
select employee_id EMPLOYEE_ID
from employees
where manager_id=1 and employee_id!=1
union
select a1.employee_id
from employees a1,
(select employee_id
from employees
where manager_id=1 and employee_id!=1) a
where manager_id=a.employee_id
union
select a2.employee_id
from employees a2,
(select a1.employee_id employee_id
from employees a1,
(select employee_id
from employees
where manager_id=1 and employee_id!=1) a
where manager_id=a.employee_id) a3
where manager_id=a3.employee_id
order by employee_id;