Sql 遞歸查詢

1,查詢指定父類下所有的子

1 with temp as
2 (
3     select * from bot_EquityFundamentals where Id=7 --父類Id
4     union all
5     select b.*from bot_EquityFundamentals b
6     inner join
7     temp t on (b.ParentId = t.Id)
8 )
9 select* from temp order by Id; 
查詢所有子類

2,查詢指定子類所有的父類

1 with temp as
2 (
3     select * from bot_EquityFundamentals where Id=2143 --子類Id
4     union all
5     select b.* from bot_EquityFundamentals b
6     inner join 
7     temp t on (b.Id=t.ParentId)
8 )
9 select * from temp;
查詢所有父類

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章