其他分享
首页 > 其他分享> > 1270

1270

作者:互联网

 

 

 

 

 方法一:自连接(我自己想到的)

# select a.employee_id
# from Employees a 
# left join Employees b 
# on a.manager_id=b.employee_id
# left join Employees c
# on b.manager_id=c.employee_id
# where c.manager_id=1
# and a.employee_id !=1;

方法二:MySql8的递归,还需要好好钻研!

WITH RECURSIVE cte  AS
(
 SELECT employee_id  FROM Employees a where employee_id !=1 and manager_id =1
  UNION ALL
  SELECT a.employee_id  FROM Employees as a join cte as b  on a.manager_id=b.employee_id  
)
SELECT * FROM cte;

没有层级限制。

 

标签:join,1270,Employees,manager,employee,id,SELECT
来源: https://www.cnblogs.com/hugrice/p/15124467.html