树的层级固定使用自连接,有几级就使用几次自连接
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 two.parentid=one.id
where one.parentid='1' and one.is_show='1' and two.is_show='1'
order by one.orderby,two.orderby;
树的层级不固定使用递归
with recursive t1 as (select *
from course_category
where id = '1'
union all
select t2.* from course_category t2
inner join t1 on t1.id = t2.parentid)
select * from t1 order by t1.id;