返回介绍

solution / 1200-1299 / 1270.All People Report to the Given Manager / README_EN

发布于 2024-06-17 01:03:21 字数 3500 浏览 0 评论 0 收藏 0

1270. All People Report to the Given Manager

中文文档

Description

Table: Employees

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| employee_id   | int   |
| employee_name | varchar |
| manager_id  | int   |
+---------------+---------+
employee_id is the column of unique values for this table.
Each row of this table indicates that the employee with ID employee_id and name employee_name reports his work to his/her direct manager with manager_id
The head of the company is the employee with employee_id = 1.

 

Write a solution to find employee_id of all employees that directly or indirectly report their work to the head of the company.

The indirect relation between managers will not exceed three managers as the company is small.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
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      |
+-------------+---------------+------------+
Output: 
+-------------+
| employee_id |
+-------------+
| 2       |
| 77      |
| 4       |
| 7       |
+-------------+
Explanation: 
The head of the company is the employee with employee_id 1.
The employees with employee_id 2 and 77 report their work directly to the head of the company.
The employee with employee_id 4 reports their work indirectly to the head of the company 4 --> 2 --> 1. 
The employee with employee_id 7 reports their work indirectly to the head of the company 7 --> 4 --> 2 --> 1.
The employees with employee_id 3, 8, and 9 do not report their work to the head of the company directly or indirectly. 

Solutions

Solution 1: Two Joins

We can use two joins to find all employees who report directly or indirectly to the company CEO.

Specifically, we first use a join to find the manager_id of the superior manager for each manager_id, and then use another join to find the manager_id of the higher-level manager. Finally, if the manager_id of the higher-level manager is $1$ and the employee_id of the employee is not $1$, it means that the employee reports directly or indirectly to the company CEO.

# Write your MySQL query statement below
SELECT e1.employee_id
FROM
  Employees AS e1
  JOIN Employees AS e2 ON e1.manager_id = e2.employee_id
  JOIN Employees AS e3 ON e2.manager_id = e3.employee_id
WHERE e1.employee_id != 1 AND e3.manager_id = 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文