当前位置:网站首页>MySQL exercise: all employees reporting to the CEO

MySQL exercise: all employees reporting to the CEO

2022-07-24 00:40:00 Avasla

subject

The employee table :Employees

Column NameType
employee_idint
employee_namevarchar
manager_idint

employee_id It's the primary key of this table .
In every row of this table ,employee_id Indicates the of the employee ID,employee_name Indicates the name of the employee ,manager_id Indicates the line manager reporting to the employee .
This company CEO yes employee_id = 1 People who .

Employees table:

employee_idemployee_namemanager_id
1Boss1
3Alice3
2Bob1
4Daniel2
7Luis4
8Jhon3
9Angela8
77Robert1

use SQL Find out all direct or indirect reports to the company CEO The report of the staff employee_id .
Because the company is small , The indirect relationship between managers does not exceed 3 Managers .
You can return results without duplicates in any order

Method 1 :UNION ALL、 Subquery

This question stipulates that the indirect relationship between managers does not exceed 3 Managers , Then we can find 3 Number of people on the floor , And finally put it all together .

1) The first floor is CEO Direct report person :

SELECT employee_id FROM Employees WHERE manager_id = 1

2) It is obvious that the second tier reporter manager_id The first floor is directly to CEO Reporting person , Therefore, you can use sub query , Find the second layer of people :

SELECT employee_id
FROM Employees WHERE manager_id IN (
    SELECT employee_id FROM Employees WHERE manager_id = 1
)

3) alike , The reporter at the third level is the one found in the second step , Directly substitute it into the self query :

SELECT employee_id
FROM Employees WHERE manager_id IN (
    SELECT employee_id FROM Employees WHERE manager_id IN (
        SELECT employee_id FROM Employees WHERE manager_id = 1
    )
)

So far, we'll find out all the reporters , Then just use UNION ALL Put them together and output them , because CEO Of manager_id Also for the 1, So the result set needs to be filtered out employee_id = 1.

SELECT DISTINCT employee_id FROM (
    SELECT employee_id
    FROM Employees WHERE manager_id = 1
    UNION ALL
    SELECT employee_id
    FROM Employees WHERE manager_id IN (
        SELECT employee_id FROM Employees WHERE manager_id = 1
    )
    UNION ALL
    SELECT employee_id
    FROM Employees WHERE manager_id IN (
        SELECT employee_id FROM Employees WHERE manager_id IN (
            SELECT employee_id FROM Employees WHERE manager_id = 1
        )
    )
) T WHERE employee_id != 1

Method 2 :JOIN

According to method 1, we know the first query manager_id Equal to the second query employee_id. According to this rule , We can use JOIN Connect the two data . adopt e2.manager_id = 1 Query all that need to be directed to CEO Reporting person ,e1.employee_id Is the data we want to query .

For the next reporting person , Do the same again JOIN that will do .

SELECT e1.employee_id
FROM Employees e1
JOIN Employees e2 ON e1.manager_id = e2.employee_id
JOIN Employees e3 ON e2.manager_id = e3.employee_id
WHERE e1.employee_id != 1 AND e3.manager_id = 1

Table situation after connection
 Insert picture description here

Method 3 、 Recursive method

Recursive usage

# Write your MySQL query statement below
with recursive temp as (
    select e.employee_id from Employees e 
    where e.employee_id!=1 and manager_id=1
    union all 
    select e.employee_id from Employees e 
    join temp t on t.employee_id=e.manager_id
)
select * from temp 

source : Power button (LeetCode)
link :https://leetcode.cn/problems/all-people-report-to-the-given-manager

原网站

版权声明
本文为[Avasla]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230737271428.html