ID | Name | ParentID |
1 | A | 0 |
2 | B | 0 |
3 | C | 1 |
4 | D | 1 |
5 | E | 3 |
6 | F | 3 |
7 | G | 5 |
8 | K | 5 |
查出A下的所有子,孙子,重孙子.......
with a as(
select id,[Name],ParentID from myTable where id=1
union ALL
select x.id,x.[Name],x.ParentID from myTable x,a
where x.ParentID=a.id
)
select * from a
其结果,就是把B去掉了,呵呵~~~
ID | Name | ParentID |
1 | A | 0 |
3 | C | 1 |
4 | D | 1 |
5 | E | 3 |
6 | F | 3 |
7 | G | 5 |
8 | K | 5 |