码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 父子关系查询脚本

时间:2018-06-11 15:50:21      阅读:427      评论:0      收藏:0      [点我收藏+]

标签:user   技术分享   node   join   inner   SQ   bsp   bubuko   图片   


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 父子关系查询脚本

标签:user   技术分享   node   join   inner   SQ   bsp   bubuko   图片   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!