表A:字段A,字段B,REMARK
10 00 0
11 10 0
12 10 1
13 11 0
14 11 1
15 12 1
16 13 0
17 16 0
18 12 0
19 16 1
字段B里的内容就是父结点。字段A是父结点号。
基本是这样一个情况。
我现在想查出一个父结点下有多少个子结点。条件是得查下三级。
也就是如果查10的话先得查出10有几个子,再看有几个孙子,再看每个孙子下面有几个儿子。
然后用儿子+孙子+孙子的儿子。这就是我想要的结果。
并不是非得用一个语句实现。
只要能查出来就行。
谢谢各位了。
分不够再加!
6 个解决方案
#1
只查三级?
#2
http://blog.csdn.net/xluzhong/articles/340852.aspx
#3
--示例数据
create table tb(字段A int,字段B int,REMARK bit)
insert tb select 10,00,0
union all select 11,10,0
union all select 12,10,1
union all select 13,11,0
union all select 14,11,1
union all select 15,12,1
union all select 16,13,0
union all select 17,16,0
union all select 18,12,0
union all select 19,16,1
go
--查询的函数
create function f_cid(@id int)
returns int
as
begin
declare @t table(id int,level int)
declare @l int
set @l=0
insert @t select 字段A,@l from tb where 字段B=@id
while @@rowcount>0 and @l<3
begin
set @l=@l+1
insert @t select a.字段A,@l
from tb a,@t b
where a.字段B=b.id and b.level=@l-1
end
return((select count(*) from @t))
end
go
--调用实现查询
select *,三代儿子个数=dbo.f_cid(字段A) from tb
go
--删除测试
drop table tb
drop function f_cid
/*--结果
字段A 字段B REMARK 三代儿子个数
----------- ----------- ------ -----------
10 0 0 9
11 10 0 5
12 10 1 2
13 11 0 3
14 11 1 0
15 12 1 0
16 13 0 2
17 16 0 0
18 12 0 0
19 16 1 0
(所影响的行数为 10 行)
--*/
create table tb(字段A int,字段B int,REMARK bit)
insert tb select 10,00,0
union all select 11,10,0
union all select 12,10,1
union all select 13,11,0
union all select 14,11,1
union all select 15,12,1
union all select 16,13,0
union all select 17,16,0
union all select 18,12,0
union all select 19,16,1
go
--查询的函数
create function f_cid(@id int)
returns int
as
begin
declare @t table(id int,level int)
declare @l int
set @l=0
insert @t select 字段A,@l from tb where 字段B=@id
while @@rowcount>0 and @l<3
begin
set @l=@l+1
insert @t select a.字段A,@l
from tb a,@t b
where a.字段B=b.id and b.level=@l-1
end
return((select count(*) from @t))
end
go
--调用实现查询
select *,三代儿子个数=dbo.f_cid(字段A) from tb
go
--删除测试
drop table tb
drop function f_cid
/*--结果
字段A 字段B REMARK 三代儿子个数
----------- ----------- ------ -----------
10 0 0 9
11 10 0 5
12 10 1 2
13 11 0 3
14 11 1 0
15 12 1 0
16 13 0 2
17 16 0 0
18 12 0 0
19 16 1 0
(所影响的行数为 10 行)
--*/
#4
declare @ID int
set @ID = 10
select 1 as level,* into #t from 表A where 字段B = @ID
insert into #t
select 2 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 1
insert into #t
select 3 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 2
select count(*) from #t
set @ID = 10
select 1 as level,* into #t from 表A where 字段B = @ID
insert into #t
select 2 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 1
insert into #t
select 3 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 2
select count(*) from #t
#5
只查三代?
#6
谢谢各位。问题解决。
重重感谢 zjcxc(邹建) 和 xluzhong(Ralph)
结贴!!!
重重感谢 zjcxc(邹建) 和 xluzhong(Ralph)
结贴!!!
#1
只查三级?
#2
http://blog.csdn.net/xluzhong/articles/340852.aspx
#3
--示例数据
create table tb(字段A int,字段B int,REMARK bit)
insert tb select 10,00,0
union all select 11,10,0
union all select 12,10,1
union all select 13,11,0
union all select 14,11,1
union all select 15,12,1
union all select 16,13,0
union all select 17,16,0
union all select 18,12,0
union all select 19,16,1
go
--查询的函数
create function f_cid(@id int)
returns int
as
begin
declare @t table(id int,level int)
declare @l int
set @l=0
insert @t select 字段A,@l from tb where 字段B=@id
while @@rowcount>0 and @l<3
begin
set @l=@l+1
insert @t select a.字段A,@l
from tb a,@t b
where a.字段B=b.id and b.level=@l-1
end
return((select count(*) from @t))
end
go
--调用实现查询
select *,三代儿子个数=dbo.f_cid(字段A) from tb
go
--删除测试
drop table tb
drop function f_cid
/*--结果
字段A 字段B REMARK 三代儿子个数
----------- ----------- ------ -----------
10 0 0 9
11 10 0 5
12 10 1 2
13 11 0 3
14 11 1 0
15 12 1 0
16 13 0 2
17 16 0 0
18 12 0 0
19 16 1 0
(所影响的行数为 10 行)
--*/
create table tb(字段A int,字段B int,REMARK bit)
insert tb select 10,00,0
union all select 11,10,0
union all select 12,10,1
union all select 13,11,0
union all select 14,11,1
union all select 15,12,1
union all select 16,13,0
union all select 17,16,0
union all select 18,12,0
union all select 19,16,1
go
--查询的函数
create function f_cid(@id int)
returns int
as
begin
declare @t table(id int,level int)
declare @l int
set @l=0
insert @t select 字段A,@l from tb where 字段B=@id
while @@rowcount>0 and @l<3
begin
set @l=@l+1
insert @t select a.字段A,@l
from tb a,@t b
where a.字段B=b.id and b.level=@l-1
end
return((select count(*) from @t))
end
go
--调用实现查询
select *,三代儿子个数=dbo.f_cid(字段A) from tb
go
--删除测试
drop table tb
drop function f_cid
/*--结果
字段A 字段B REMARK 三代儿子个数
----------- ----------- ------ -----------
10 0 0 9
11 10 0 5
12 10 1 2
13 11 0 3
14 11 1 0
15 12 1 0
16 13 0 2
17 16 0 0
18 12 0 0
19 16 1 0
(所影响的行数为 10 行)
--*/
#4
declare @ID int
set @ID = 10
select 1 as level,* into #t from 表A where 字段B = @ID
insert into #t
select 2 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 1
insert into #t
select 3 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 2
select count(*) from #t
set @ID = 10
select 1 as level,* into #t from 表A where 字段B = @ID
insert into #t
select 2 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 1
insert into #t
select 3 as level,a.* from 表A a,#t b where a.字段B = b.字段A and b.level = 2
select count(*) from #t
#5
只查三代?
#6
谢谢各位。问题解决。
重重感谢 zjcxc(邹建) 和 xluzhong(Ralph)
结贴!!!
重重感谢 zjcxc(邹建) 和 xluzhong(Ralph)
结贴!!!