高分 求一查询
表: coltext
列:
col001 | col002 | col003 | col004
--------------------------------------------
100001 | 类别1 | 000000 | 类别0
--------------------------------------------
100002 | 类别3 | 000001 | 类别1
--------------------------------------------
100003 | 类别4 | 000002 | 类别3
--------------------------------------------
100100 | 类别100 | 000003 | 类别4
--------------------------------------------
100101 | 类别101 | 100100 | 类别100
--------------------------------------------
100102 | 类别102 | 000003 | 类别4
就是说 col001 是父 col003 是子,无限级别,现数据在100W以上.
如果想查出: col001 = 000001 的所有下级类别该怎么做,
比较笨,想不出好方法来,高分求.
谢谢关注.
问题点数:100、回复次数:11Top
1 楼LouisXIV(夜游神)回复于 2006-07-04 02:18:26 得分 10
--树形数据查询示例
--作者: 邹建
if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GO
--示例数据
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
insert [tb] select 0,'中国'
union all select 0,'美国'
union all select 0,'加拿大'
union all select 1,'北京'
union all select 1,'上海'
union all select 1,'江苏'
union all select 6,'苏州'
union all select 7,'常熟'
union all select 6,'南京'
union all select 6,'无锡'
union all select 2,'纽约'
union all select 2,'旧金山'
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_cid]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_cid]
GO
/*--树形数据处理
查询指定id的所有子
--邹建 2003-12(引用请保留此信息)--*/
/*--调用示例
--调用(查询所有的子)
select a.*,层次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
--*/
create function f_cid(
@id int
)returns @re table([id] int,[level] int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l
from [tb] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
/*--如果只显示最明细的子(下面没有子),则加上这个删除
delete a from @re a
where exists(
select 1 from [tb] where [pid]=a.[id])
--*/
return
end
go
--调用(查询所有的子)
select a.*,层次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_pid]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_pid]
GO
/*--树形数据处理
查询指定id的所有父
--邹建 2003-12(引用请保留此信息)--*/
/*--调用示例
--调用(查询所有的父)
select a.* from [tb] a,f_pid(7)b where a.[id]=b.[id]
--*/
create function f_pid(
@id int
)returns @re table([id] int,[level] int)
as
begin
declare @l int
set @l=0
insert @re select [pid],@l from [tb] where [id]=@id and [pid]<>0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[pid],@l
from [tb] a,@re b
where a.[id]=b.[id] and b.[level]=@l-1 and a.[pid]<>0
end
return
end
go
--调用(查询所有的父)
select a.* from [tb] a,f_pid(7)b where a.[id]=b.[id]
go
Top
2 楼ilyvmly(ilyvmly)回复于 2006-07-04 08:14:56 得分 10
用递归函数。也就是说在应用程序里定义一个函数是查询父为某个条件的子的数量和子做为下一级父记录,对这些记录调用函数本身,也就是递归调用。就可以了。我想单单在 MS-SQL Server里用sql语句是做不到的。至少是要在应用程序里用教本控制结合sql语句。Top
3 楼sxycgxj(云中客)回复于 2006-07-04 08:19:03 得分 10
有几个问题:
1.col001 = 000001 ,好像楼主列出的数据中没有这个000001,请问你指的是哪个
2.列表中的父子关系是用哪个列来确定的,是如何确定的Top
4 楼zjdyzwx(十一月猪)回复于 2006-07-04 09:43:34 得分 10
我想楼主如果没有层次的话用游标Top
5 楼zjdyzwx(十一月猪)回复于 2006-07-04 10:49:12 得分 10
declare @t table(par int , chi int)
insert into @t
select 1 , 2 union
select 2 , 3 union
select 2 , 4 union
select 3 , 5 union
select 3 , 7
select * from @t
declare @cnt int
--declare cursor1 scroll cursor for select par , chi from @t order by par
--open cursor1
--fetch next from cursor1 into @par, @chi
--while @@
create table #
(id int identity(1,1) , chi int)
insert into #
select chi from @t where par = 1
select @cnt = @@rowcount
while @cnt > 0
begin
insert into #
select chi from @t where par in (select chi from # where id > (select max(id)- @cnt from #))
select @cnt = @@rowcount
end
select chi from #
Top
6 楼fcuandy(了此残生.)回复于 2006-07-04 11:03:13 得分 10
col001跟 col003的意思:
id parentid
1 0
2 0
3 1
4 1
5 3
跟这没实质区别,这种问题,好像问的太多了吧Top
7 楼paoluo(一天到晚游泳的鱼)回复于 2006-07-04 11:17:25 得分 10
zjdyzwx(十一月猪) ,“数据在100W以上”,你用游標處理,想想會是怎樣的狀況。Top
8 楼Haiwer(海阔天空)回复于 2006-07-04 11:25:24 得分 10
1、sql函数支持递归
2、递归函数都是可以用循环代替的
3、“就是说 col001 是父 col003 是子”好像与你的例子不符
Top
9 楼Trance(今何安)回复于 2006-07-04 20:00:30 得分 0
select col003, col004 from coltext
where col001 = 000001
这样不行吗?Top
10 楼guohuasky(梦幻)回复于 2006-07-05 17:53:36 得分 0
不理想,我不是查询不出来,是想要个快点的方法.Top
11 楼bugchen888(臭虫)回复于 2006-07-05 19:35:41 得分 20
--创建函数
create function dbo.get_all_children
(@parent as nvarchar(1000))
returns table table_temp(class_id nvarchar(1000),class_name nvarchar(1000),level int)
as
begin
declare @r int
declare @level int
set @level=1
insert into table_temp
select col003,col004,@level from coltext where col001=@parent
set @r=@@rowcount
while @r>0
begin
insert into table_temp
select col003,col004,@level+1 from coltext
where col001 in (select class_id from table_temp where level=@level)
set @r=@@rowcount
set @level=@level+1
end
return
end
--调用函数即可
select * from dbo.get_all_children('000001')Top




