sql server 递归查询父节点、子节点

查询所有子节点

with t as
(
  select b.* from IPS_ParkinglotRel b where parentParkingLot = 7
  union all
  select a.* from IPS_ParkinglotRel a join t b on a.parentParkingLot=b.parkingLotId
)
select * from t

 

 

查询所有父节点

with t2 as
(
  select b.* from IPS_ParkinglotRel b where childParkingLot = 7
  union all
  select a.* from IPS_ParkinglotRel a join t2 b on a.childParkingLot=b.parkingLotId
)
select * from t2

 

7代表的是要查的节点

 

posted on 2021-06-28 16:37  炼金师  阅读(611)  评论(0)    收藏  举报

导航