SQL Server 父子关系查询脚本


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
)

 

 

posted @ 2018-06-11 15:02  我知道你的存在  阅读(459)  评论(0编辑  收藏  举报