SQL Server 父子关系查询脚本

时间:2018-06-11 15:18:14   收藏:0   阅读:247


with nodes
as (
select * from users as child where child.id = 6
union all
select par.* from users as par
inner join nodes as rc on par.id = rc.pid
)
select * from users where id in (
select id from nodes n
)

 

 技术分享图片

 

-- //

with nodes
as (
select * from users as par where par.id = 2
union all
select child.* from users as child
inner join nodes as rc on child.pid = rc.id
)
select * from users where id in (
select id from nodes n
)

 

技术分享图片

 

原文:https://www.cnblogs.com/chenliang-zibo/p/9167097.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!