mysql 8递归查询
带有排序和层级
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 allselect 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
向上查
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
向下查询
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
查询孩子节点数量
with recursive cte as(select * from com_office_project_wbs where id = '1942822902508290048'union allselect 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'