1

我有这个 4 级用户表,如下所示

user_id parent_id
1       NULL
2       1
3       2
4       2
5       3
6       1

我想选择某个父 id 下的所有用户,例如 user_id = 1,在这种情况下应返回如下结果:

user_id  parent_id
2        1         // return because parent_id = 1
6        1         // return because parent_id = 1
3        2         // return because parent_id of 2 is 1
4        2         // return because parent_id of 2 is 1
5        3         // return because parent_id of 3 is 2

通过提供一个 id,查询会是什么?

提前致谢!

4

2 回答 2

0

试试这个 ::

    Select 
user_id 
from 

my_table 
where parent_id in 
(
Select 
user_id 
from my_table where parent_id in 
(Select user_id from my_table where parent_id in(Select user_id from my_table where parent_id =?)))
于 2012-12-04T07:29:46.650 回答
0
parentid as refid 
Select userid  from SampleTree where refid = 0 or refid in (Select userid from SampleTree where refid =0 
or refid in (Select userid from SampleTree where refid =0 or
refid in (Select userid from SampleTree where refid =0)))
于 2013-03-26T08:13:26.920 回答