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'
相关推荐
Navicat中国3 分钟前
Navicat x 达梦技术指引 | 模型转换
数据库·达梦·navicat·概念模型·物理模型·逻辑模型·模型转换
邂逅星河浪漫4 分钟前
【MySQL 事务】详细介绍+实例
数据库·mysql·事务
tianyuanwo7 分钟前
RPM数据库锁竞争:原理、诊断与根治方案
数据库·rpm·bdb·命令挂起
TG:@yunlaoda360 云老大9 分钟前
如何评估华为云国际站代理商跨境合规要求?
大数据·数据库·华为云·云计算
志凌海纳SmartX10 分钟前
银行核心系统备库“降本增效”探索:超融合承载Oracle ADG备库的测试验证
数据库·oracle
木卫二号Coding13 分钟前
POSTGRESQL+数据库备份与还原
数据库·postgresql·oracle
Lvan的前端笔记14 分钟前
python:用 dotenv 管理环境变量&生产环境怎么管理环境变量
网络·数据库·python
云老大TG:@yunlaoda36015 分钟前
如何通过华为云国际站代理商OBS实现数据跨境传输与分发加速?
数据库·华为云·php
梓仁沐白15 分钟前
CSAPP-Archlab
数据库·windows
-大头.19 分钟前
SQL性能优化与索引策略实战
数据库·sql·性能优化