问个无限极分类的SQL查询

来源:百度知道 编辑:UC知道 时间:2024/06/02 05:58:41
id parentid
1 0
2 0
3 1
4 2
5 2
6 1
7 1
8 4
9 4
10 6
11 5
SQL怎么找出2以下的所有子级
4,5,8,9,11

多加个字段 来查询 嘿嘿

select distinct parentid where parentid>2

代码如下:

SQL> select * from test;

ID PARENTID
---------- ----------
1 0
2 0
3 1
4 2
5 2
6 1
7 1
8 4
9 4
10 6
11 5

11 rows selected

SQL>
SQL> select id
2 from test
3 start with parentid=2
4 connect by parentid = prior id;

ID
----------
4
8
9
5
11

这个要用循环来写吧.用存储过过程.

select id from 表 where parentit=2 or parentid in (select id from 表 where parentit=2)

如果是无限级别的子集下去,就要用循环了

declare @t table(id int)
insert into @t values(2)
while exists (select