首页 > 数据库技术 > 详细

SQL Server 父子关系查询脚本

时间:2018-06-11 15:18:14      阅读:246      评论:0      收藏:0      [点我收藏+]


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
)

 

技术分享图片

 

SQL Server 父子关系查询脚本

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!