怎么查找无级分类中一基础大类下子类的最新信息
数据库有两个表一个是class(无级分类的),一个是info表。
class表
------------------
id parent_id classname islast
1 0 类一 1
2 1 类二 0
3 1 类三 1
4 2 类四 0
5 4 类五 1
info表
------------------
id classid name
1 1 X
2 2 T
3 1 Y
4 2 Z
5 3 C
6 1 T
我想生成这样一页,查找基础类1下面的所有子类,如果该子类是最终类就查找最新的5条,如果该子类不是最终类,那就从该子类的子类中查找最新的5条记录
基础类1
__________________________________
类二 类三
_________________________________
问题点数:50、回复次数:10Top
1 楼zjcxc(邹建)回复于 2005-06-03 10:00:30 得分 50
--查询处理的函数
create function f_cid(@id int)
returns @re table(id int,level int,islast bit)
as
begin
declare @l int
set @l=0
insert @re select id,@l,islast from class where id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,a.islast
from class a,@re b
where a.parent_id=b.id and b.level=@l-1
end
delete from @re where islast=1
end
go
--调用函数实现查询(查询基础1)
select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid
Top
2 楼hdyong(无痕)回复于 2005-06-03 11:01:35 得分 0
显示有错误,说是最后一条语句必须是返回语句。Top
3 楼zjcxc(邹建)回复于 2005-06-03 11:14:01 得分 0
--查询处理的函数
create function f_cid(@id int)
returns @re table(id int,level int,islast bit)
as
begin
declare @l int
set @l=0
insert @re select id,@l,islast from class where id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,a.islast
from class a,@re b
where a.parent_id=b.id and b.level=@l-1
end
delete from @re where islast=1
return --少了
end
go
--调用函数实现查询(查询基础1)
select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid
Top
4 楼hdyong(无痕)回复于 2005-06-03 11:20:26 得分 0
还是出不来。
我把select top 5 a.* from info i,f_cid(1) b where a.classid=b.classid
改成
select top 5 a.* from info a,f_cid(1) b where a.classid=b.id
是通过了,但是什么也查不出来。Top
5 楼zjcxc(邹建)回复于 2005-06-03 11:24:16 得分 0
--1.
select f_cid(1) --看看是否查出了1的所有子类
--2.你列的示例数据有些不对吧? id=1的,它应该不是最末级,islast怎么会是1呢?Top
6 楼zjcxc(邹建)回复于 2005-06-03 11:26:52 得分 0
create table class(id int,parent_id int,classname varchar(10),islast bit)
insert class select 1,0,'类一',1
union all select 2,1,'类二',0
union all select 3,1,'类三',1
union all select 4,2,'类四',0
union all select 5,4,'类五',1
create table info(id int,classid int,name varchar(10))
insert info select 1,1,'X'
union all select 2,2,'T'
union all select 3,1,'Y'
union all select 4,2,'Z'
union all select 5,3,'C'
union all select 6,1,'T'
go
--查询处理的函数
create function f_cid(@id int)
returns @re table(id int,level int,islast bit)
as
begin
declare @l int
set @l=0
insert @re select id,@l,islast from class where id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,a.islast
from class a,@re b
where a.parent_id=b.id and b.level=@l-1
end
delete from @re where islast=0
return
end
go
--调用函数实现查询(查询基础1)
select top 5 a.* from info a,f_cid(1) b where a.classid=b.id
go
--删除测试
drop table class,info
drop function f_cid
/*--结果
id classid name
----------- ----------- ----------
1 1 X
3 1 Y
5 3 C
6 1 T
(所影响的行数为 4 行)
--*/Top
7 楼zjcxc(邹建)回复于 2005-06-03 11:27:21 得分 0
数据删除的写反了,正确的如上Top
8 楼hdyong(无痕)回复于 2005-06-03 12:02:14 得分 0
不好意思,前面的数据是有错误。
Top
9 楼hdyong(无痕)回复于 2005-06-03 13:06:14 得分 0
class表
------------------
id parent_id classname islast
1 0 类一 0
2 1 类二 0
3 1 类三 1
4 2 类四 0
5 4 类五 1
5 1 类六 1
info表
------------------
id classid name
1 1 X
2 2 T
3 1 Y
4 2 Z
5 3 C
6 1 T
我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于类一下的哪个字类的的classid,
也就是这样的,那应该怎么修改?
id classid name
----------- ----------- ---------- ----------
1 1 X
3 1 Y
5 3 C
6 1 TTop
10 楼hdyong(无痕)回复于 2005-06-03 13:10:44 得分 0
修改了一下,
class表
------------------
id parent_id classname islast
1 0 类一 0
2 1 类二 0
3 1 类三 1
4 2 类四 0
5 4 类五 1
6 1 类六 1
info表
------------------
id classid name
1 3 X
2 5 T
3 4 Y
4 6 Z
5 3 C
6 5 T
我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于"类一"下的哪个子类的的classid,
也就是这样的
id classid name classid2
----------- ----------- ---------- ----------
1 3 X 3
3 4 Y 2
5 3 C 3
6 5 T 2Top




