-- 获得上级及上上级
with cte as(select id,name,parent_id,0 as lvl from catelog where id = '1001'
union all
select d.id,d.name,d.parent_id,lvl + 1 from cte c inner join catelog d on c.parent_id = d.id)
select * from cte
-- 获得下级及下下级
with cte as(select id,name,parent_id,0 as lvl from catelog where id = '1001'
union all
select d.id,d.name,d.parent_id,lvl + 1 from cte c inner join catelog d on c.id = d.parent_id)
select * from cte
转载请注明原文地址: https://ju.6miu.com/read-664754.html