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 则是一个递归的查询子句,他会把查询出来的结果再次代入到查询子句中继续查询,如下面的语句
复制代码

 

参考地址

posted @   译林  阅读(177)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示