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;
查询所有父类

原文地址:https://www.cnblogs.com/shangec/p/14149330.html