mysql8 递归子查询实现
WITH RECURSIVE t1 可以实现递归查询出所有的数据 (向上递归/向下递归)
with recursive t1 as (
select * from sys_dept where dept_leader = 1 and delete_flag = 0
union all
select t.* from sys_dept t inner join t1 on t1.dept_id = t.parent_id and t.delete_flag = 0
)
select * from t1;
说明:
sql中with xxxx as () 是对一个查询子句做别名,同时数据库会对该子句生成临时表;
with recursive 则是一个递归的查询子句,他会把查询出来的结果再次代入到查询子句中继续查询,如下面的语句