方法一:自连接(我自己想到的)
# 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;
没有层级限制。