sql 获取指定节点的所有父节点或者所有子节点
获取节点的所有父节点
;with
#tmp as(
select * from tb
where id = 'DMA20120327036'
union all
select a.* from tb a, #tmp b
where a.id = b.pid
)
select * from #tmp
获取节点的所有子节点
;with
#tmp as(
select * from tb
where id = 'DMA20120327036'
union all
select a.* from tb a, #tmp b
where a.pid = b.id
)
select * from #tmp
本文介绍使用SQL的with语句来查询特定节点的所有父节点或所有子节点的方法。通过递归的方式,能够有效地获取层级结构中指定节点的上下级信息。
5663

被折叠的 条评论
为什么被折叠?



