请教sql语句查询,在线等。。。

来源:百度知道 编辑:UC知道 时间:2024/06/05 19:15:37
我有一张类型表,里面的字段是级联的,意思就是说最顶部的一个类型的Id是它的子类的引用的Id,而子类的Id又是它的下一级子类的Id,就这样连接着的,我想根据最顶的类型Id查询它的所有的子类型的类表Id,请问下怎么查,谢谢了。。。

该问题在 http://zhidao.baidu.com/question/109978781.html 已有类似回答,完全是送分啊^_^
首先,创建一个函数,判断子类是否在父类下,是返回1,否返回0,如子节点与父节点为同一节点返回1
CREATE FUNCTION [dbo].[TestChildClass]
(@ChildId int,@Fatherid int )
RETURNS bit
AS
BEGIN
declare @i int
declare @n int
set @i=isnull(@ChildId,0)
set @n=0

while @i>0
begin
if @i=isnull(@fatherid,0)
begin
set @n=1
break
end
set @i=(select fatherID from 表1 where ID=@i)
end

return @n
END

接下来就简单了:
select *
from 表1
where dbo.TestChildClass(某ID,父ID)=1

想直接查恐怕有点麻烦,你可以在百度上HI一下我,咱们聊聊吧

oracle,经过测试:
sql@kokooa>select * from test006;

ID PARENTID
---------- ----------
1 0
2 0