一个表中有多条数据,要通过一条数据的id查找这条数据的下级数据(下级数据中包含上级数据的id号,下级数据可能包含的还有下级数据) 怎么通过sql语句查找数据
2 回答
PIPIONE
TA贡献1829条经验 获得超9个赞
--查询表dealers中dealerID为100的所有子dealer.
Declare @DealerID int
set @DealerID = 100;
with ChildrenDealer(DealerID,DealerName) as
(
select DEALERID, DEALERName from dealers where DEALERID=@DealerID
union All
select a.DEALERID, DEALERName from dealers a inner join ChildrenDealer b on a.PARENTID = b.DealerID
)
select * from ChildrenDealer
- 2 回答
- 0 关注
- 481 浏览
添加回答
举报
0/150
提交
取消