关于SQL查询最后一个子节点的问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 05:40:33
现有亲子关系表A(父ID,子ID)
数据如下:
父ID 子ID
1 11
1 12
2 21
11 111
11 112
21 212
21 213
现在想通过ID=2,来检索2下面的所有叶节点(不需要中间的节点)
结果想显示如下:
ID1 ID2
2 212
2 213
哪位大虾知道的请指教一下啊!
谢谢!
上面的不仅仅是三层关系,各个层数不定的

如果ID为1的话,检索结果要求如下
ID1 ID2
1 12
1 111
1 112

CREATE TABLE fathson
(
父ID int,
子ID int
)

insert into fathson values('1','11')
insert into fathson values('1','12')
insert into fathson values('2','21')
insert into fathson values('11','111')
insert into fathson values('11','112')
insert into fathson values('21','212')
insert into fathson values('21','213')

select a.父ID ID1,b.子ID ID2
from fathson a,fathson b
where a.子ID=b.父ID and a.父ID='2'

题目说的不清楚???????
有个问题,如果是ID=1
结果是这样吗?
结果想显示如下:
ID1 ID2
1 12
1 111
1 112

例如:表a
pid cid
1 11
1 12
11 111
11 112
112 1121
112 1122
2 21
2 22
存储过程:
create proc getChild
@pid varchar(10)
a