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'
相关推荐
Hx__5 小时前
Redis对象编码
数据库·redis·缓存
运维帮手大橙子6 小时前
完整的登陆学生管理系统(配置数据库)
java·前端·数据库·eclipse·intellij-idea
0wioiw07 小时前
Redis(④-消息队列削峰)
数据库·redis·缓存
Runing_WoNiu7 小时前
Mysql与Ooracle 索引失效场景对比
数据库·mysql·oracle
beijingliushao7 小时前
32-Hive SQL DML语法之查询数据
数据库·hive·sql
JIngJaneIL8 小时前
专利服务系统平台|个人专利服务系统|基于java和小程序的专利服务系统设计与实现(源码+数据库+文档)
java·数据库·小程序·论文·毕设·专利服务系统平台
__风__8 小时前
windows 上编译PostgreSQL
数据库·postgresql
木木子99998 小时前
数据库范式
数据库
涛思数据(TDengine)8 小时前
通过最严时序标准,再登产业图谱榜首,TDengine 时序数据库在可信数据库大会荣获双荣誉
大数据·数据库·时序数据库·tdengine·涛思数据