LeetCode-1270. Everyone's reporting to the CEO (middle)

Employees Table: Employees

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| employee_id | int |
| Employee_name | VARCHAR |
| manager_id | int |
+ --------------- + -------- - +
the employee_id is the primary key of this table.
Each row in this table, employee_id ID represents the workers, employee_name represents the name of the workers, manager_id express line managers reporting of the trade union.
The company CEO is the person employee_id = 1.
 

SQL query with a employee_id all employees working directly or indirectly Report to the CEO.

Due to the small size of the company, the indirect relationship between managers not more than three managers.

The results may be returned in any order, you do not need to weight.

Example query results are as follows:

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           |
+-------------+

Employee_id company CEO is 1.
employee_id 2 and 77 staff reporting directly to company CEO.
4 employee_id indirect reporting staff to the company CEO 4 -> 2 -> 1 .
7 employee_id indirect reporting staff to the company CEO 7 -> 4 -> 2 -> 1.
employee_id staff is 3, 8, 9 will not report directly or indirectly to the company CEO. 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/all-people-report-to-the-given-manager
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: SQL query with a employee_id all employees working directly or indirectly Report to the CEO.

Thoughts: Query report directly to the CEO, and then continue to look for people who report to the CEO reports, and so on. Up to three levels.

Problem solving:

Idea: Work process three times to report to the manager to find the corresponding three times, each time the results of two union together to find a;

Specific implementation:
First look: through leadership id = 1, reporting directly to the owner to find staff I id = 2 = 77 and the above mentioned id,
of Union
second look: The first time the query results, I find the report to the staff of the above mentioned id = 4 employees II,
of Union
third Finding: the results of the second query, the report found the staff to staff II III id = 7.

The last will be aligned to the size of the number of employees.

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
-- 练习
-- 查询直接向经理汇报的。
select employee_id from employee where manger_id = 1 and employee_id!=1  union
-- 继续查询
select a1.employee_id from employee a1,
-- 查询第一次结果
(select employee_id from employees where manger_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,()a where manger_id = a.employee_id)a3 where manger_id = a3.employee_id 
order by employee_id;

Method Two:

select e1.employee_id
from Employees e1 
left join Employees e2 on e1.manager_id = e2.employee_id
left join Employees e3 on e2.manager_id = e3.employee_id
where e3.manager_id=1 and e1.employee_id<>1;

-- 练习
select e1.employee_id from employees e1
left join employee e2 on e1.manger_id = e2.employee_id
left join employee e3 on e2.manager_id = e3.employee_id
where e3.manager_id=1 and e1.employee_id<>1;

 Knowledge points:

Published 144 original articles · won praise 2 · Views 5739

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104751038