树形表的标记字段是什么?如何查询树形表?

发布时间 2023-03-23 16:43:36作者: 嗝`

树形表的标记字段是什么

是parentID即父节点的id

如何查询树形表

  1. 当层级固定的时候可以用表的自连接查询
select 
	one.id one_id,
	one.label one_label,
	two.id two_id,
	two.label two_label
	from course_category one INNER JOIN course_category two on one.id=two.parentid 
where one.parentid='1' 
	and one.is_show='1'
	and two.is_show='1'
ORDER BY one.orderby,two.orderby;
  1. 如果想要灵活查询每个层级可以使用mysql中的递归方法,使用with recursive。
-- 递归查询
with recursive t1 as (
            select * from  course_category p where  p.id= '1'
            union all
            select t.* from course_category t inner join t1 on t1.id = t.parentid
        )
        select *  from t1 order by t1.id, t1.orderby;