sql server 递归查询父节点、子节点 - 炼金师

时间:2024-03-11 18:24:21

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代表的是要查的节点