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
)

猜你喜欢

转载自www.cnblogs.com/chenliang-zibo/p/9167097.html
今日推荐