CEO(中央)にLeetCode-1270。みんなの報告

従業員表:従業員

+ --------- + --------------- +
|カラム名|タイプ|
+ --------------- + + ---------
| EMPLOYEE_ID | int型|
| EMPLOYEE_NAME | VARCHAR |
| MANAGER_ID | int型|
+ --------------- + -------- - +
EMPLOYEE_IDは、このテーブルの主キーです。
この表の各行は、EMPLOYEE_ID IDは、労働組合の報告ラインマネージャーを発現MANAGER_ID、EMPLOYEE_NAMEは労働者の名前を表し、労働者を表します。
同社の最高経営責任者(CEO)は、人のEMPLOYEE_ID = 1です。
 

SQLクエリがCEOに直接または間接的にレポート働くすべての従業員をEMPLOYEE_ID。

同社の小型サイズ、間接的な経営者との関係ではないつ以上のマネージャへ。

結果は、任意の順序で返されることがあり、あなたは体重をする必要はありません。

次のように例のクエリ結果は以下のとおりです。

従業員のテーブル:
+ ------------- + --------------- + ------------ +
| EMPLOYEE_ID | EMPLOYEE_NAME | MANAGER_ID |
+ ------------- + --------------- + ------------ +
| 1 | ボス| 1 |
| 3 | アリス| 3 |
| 2 | ボブ| 1 |
| 4 | ダニエル| 2 |
| 7 | ルイス| 4 |
| 8 | JHON | 3 |
| 9 | アンジェラ| 8 |
| 77 | ロバート| 1 |
+ ------------- + --------------- + ------------ +

テーブルの結果:
+ ------------- +
| EMPLOYEE_ID |
+ ------------- +
| 2 |
| 77 |
| 4 |
| 7 |
+ ------------- +

EMPLOYEE_ID会社の最高経営責任者(CEO)は1である
会社のCEOに直接報告EMPLOYEE_ID 2と77のスタッフ。
- > 2 - > 1 4は、会社の最高経営責任者(CEO)4への間接的な報告のスタッフをEMPLOYEE_ID 。
> 4 - - > 2 7は、同社の最高経営責任者(CEO)7への間接的な報告のスタッフをEMPLOYEE_ID - > 1。
EMPLOYEE_IDスタッフが3、8、9、会社の最高経営責任者(CEO)に直接または間接的に報告しません。 

出典:滞在ボタン(LeetCode)
//leetcode-cn.com/problems/all-people-report-to-the-given-manager:リンク:httpsの
すべてのネットワークからの控除が著作権を保有。商業転載は、ソースを明記してください許可公式、非商用の転載をご連絡ください。

モデレーション:付きSQLクエリは、すべての従業員がCEOに直接または間接的にレポート作業EMPLOYEE_ID。

その後、CEOに直接クエリレポート、および最高経営責任者(CEO)の報告書に報告し、その上の人を探し続け:思考。3つのレベルまで。

問題解決:

アイデア:ワークプロセスは三回には、対応する3回、2つの組合の結果を一緒に見つけるために、それぞれの時間を見つけるために、管理者に報告します。

特定の実装:
まず見て:リーダーシップID = 1を通じて、スタッフのI = 2 = 77 idと上記ID、見つけるために、所有者に直接報告
連合の
第二の外観:初めてクエリ結果は、私はスタッフにレポートを見つけます上述ID上記= 4人の従業員II、
連合の
第三の発見:第二のクエリの結果は、レポートは、スタッフII IIIのID = 7にスタッフを発見しました。

最後には、従業員の数の大きさに合わせます。

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;

方法2:

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;

 知識ポイント:

公開された144元の記事 ウォンの賞賛2 ビュー5739

おすすめ

転載: blog.csdn.net/Hello_JavaScript/article/details/104751038