mysql 8递归查询

带有排序和层级

bash 复制代码
 with recursive cte as
        (
        select *, cast(order_num as char(1000)) as sort_path, 1 as level from com_office_project_wbs where id = '1942822902508290048'
        union all
        select c.*, concat(cte.sort_path, '-', c.order_num), cte.level + 1  from com_office_project_wbs c, cte where c.parent_id = cte.id
        )
        select t.sort_path,t.level, t.* from cte t 
ORDER BY sort_path

向上查

bash 复制代码
with recursive cte as
(
select * from com_office_project_wbs where id = '1942824126712381440'
union all
select c.* from com_office_project_wbs c, cte where c.id = cte.parent_id
)
select * from cte t 

向下查询

bash 复制代码
	 with recursive cte as
(
select * from com_office_project_wbs where id = '1942822902508290048'
union all
select c.* from com_office_project_wbs c, cte where c.parent_id = cte.id
)
select * from cte t 

查询孩子节点数量

bash 复制代码
with recursive cte as
           (
               select * from com_office_project_wbs where id = '1942822902508290048'
               union all
               select c.* from com_office_project_wbs c, cte where c.parent_id = cte.id
           )
    select IFNULL(count(1),0) cn from cte where id != '1942822902508290048'
相关推荐
dishugj19 分钟前
iSCSI + Multipath + ASM:Oracle RAC 共享存储技术链详解
数据库·oracle
yoothey43 分钟前
MySQL事务机制解析 - 面试高分知识点
数据库·mysql·面试
Lkstar1 小时前
万字长文Query改写与多路召回实战|从HyDE到RRF融合,召回率提升22%的完整方案
数据库·人工智能·llm
IT新视界2 小时前
星环科技ArgoDB:基于一体化架构构建数据全生命周期安全底座
数据库·科技·安全·架构
峥无2 小时前
MySQL DML 操作(CRUD)总结
数据库·mysql
数据库小学妹2 小时前
SQL Server数据库同步工具怎么选?6款方案对比+信创迁移避坑清单
数据库·经验分享·sqlserver·dba
不剪发的Tony老师2 小时前
国产数据库之GaussDB:固若金汤
数据库·gaussdb
雨辰AI3 小时前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
凡人叶枫4 小时前
Effective C++ 条款39:明智而审慎地使用 private 继承
java·数据库·c++·嵌入式开发
基德爆肝c语言4 小时前
MySQL表的操作
前端·数据库·mysql