sql server 子找父和父找子

父找子

with
RTD1 as(
select Id,pid from Sys_XCode
),
RTD2 as(
select * from RTD1 where id=1
union all
select RTD1.* from RTD2 inner join RTD1
on RTD2.id=RTD1.PID
)
select * from RTD2

 

子找父

with   
    RTU1 as(  
        select id ,pid from UserGroup  
    ),  
    RTU2 as(  
        select * from RTU1 where id=26  
        union all  
        select RTU1.* from RTU2 inner join RTU1   
        --on myT2.id=myT.PID  
        on RTU2.PID=RTU1.ID  
    )   
select * from RTU2  
posted @ 2013-09-18 11:58  永远没有尽头  阅读(253)  评论(0编辑  收藏  举报