无限分类如何实现通过子类查找上级所属的所有父类?

dreamweaverjsf 2011-02-16 05:24:25
本人采用三个字段建的分类表:id,fid,name
想实现网站的“当前位置”功能,也就是通过子类查出父类,页不是通过父类查出下面的子类。

如:百度知道 > 电脑/网络 > 程序设计

不知道如何实现
...全文
564 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmlife 2011-02-16
  • 打赏
  • 举报
回复
递归调用。。。

(fid是父类ID吗??如果是,然后:)
由当前分类获取 fid ,逐级向上查询,直到fid=0退出
-晴天 2011-02-16
  • 打赏
  • 举报
回复
create table tb(id int,fid int,name nvarchar(10))
insert into tb select 1,0,'百度知道'
insert into tb select 2,1,'电脑/网络'
insert into tb select 3,2,'程序设计'
insert into tb select 4,3,'数据库程序设计'
insert into tb select 5,1,'电脑/单机'
go
declare @current nvarchar(10)
set @current='程序设计'
;with cte as(
select * from tb where name=@current
union all
select a.* from tb a inner join cte b on a.id=b.fid
),c1 as(
select top 100 name from cte order by fid
)
select stuff((select '_'+[name] from c1 for xml path('')),1,1,'')
go
drop table tb
/*
--------------------------------------------------------------------------------------
百度知道_电脑/网络_程序设计

(1 行受影响)

*/
-晴天 2011-02-16
  • 打赏
  • 举报
回复
create table tb(id int,fid int,name nvarchar(10))
insert into tb select 1,0,'百度知道'
insert into tb select 2,1,'电脑/网络'
insert into tb select 3,2,'程序设计'
insert into tb select 4,3,'数据库程序设计'
insert into tb select 5,1,'电脑/单机'
go
declare @current nvarchar(10)
set @current='程序设计'
;with cte as(
select * from tb where name=@current
union all
select a.* from tb a inner join cte b on a.id=b.fid
),c1 as(
select top 100 name from cte order by fid desc
)
select stuff((select '_'+[name] from c1 for xml path('')),1,1,'')
go
drop table tb
/*
--------------------------------------------------------------------------------------
程序设计_电脑/网络_百度知道

(1 行受影响)

*/

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧