当前位置:网站首页>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 Name | Type |
|---|---|
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
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_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 |
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 
Method 3 、 Recursive method
# 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
边栏推荐
- Gbase 8C session information function (6)
- JS drag and drop element
- How can dbcontext support the migration of different databases in efcore advanced SaaS system
- PayPal subscription process and API request
- MySQL table field quantity limit and row size limit
- How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
- Detectron2 installation based on Anaconda under win10
- Flutter | the easiest way to add header and footer to listview
- Don't let Fujin Yibo see this
- Gbase 8C session information function (4)
猜你喜欢

Inode, soft link, hard link

Comparison of the shortcomings of redis master-slave, sentinel and cluster architectures

English语法_指示代词 - So

Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)

MariaDB database upgrade version

English语法_指示代词 -such / the same

Treatment of particle boundary collision

Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot

Coloring old photos - deoldify get started quickly

Generic mechanism and enhanced for loop
随机推荐
Gbase 8C access authority query function (I)
ES6 combines multiple class methods
Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!
GBase 8c 访问权限查询函数(三)
JS drag and drop element
MySQL data query (select)
Communication module sorting (II) hc-05
How can dbcontext support the migration of different databases in efcore advanced SaaS system
Redis cluster hash sharding algorithm (slot location algorithm)
Method of C language annotation
XXL job realizes the code parsing of email sending warnings (line by line code interpretation)
GBase 8c模式可见性查询函数(二)
QT入门篇(2.1初入QT的开始第一个程序)
采坑websocket總結
MariaDB database upgrade version
Redis distributed lock to be continued
泛型机制和增强for循环
High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance
Case error of MySQL branch statement
AWS Part 4 one machine and one secret