无级树查询问题求助兼散分

jonescheng 2005-02-25 09:10:12
分类采用了无级树的结构:
如下:
-交通工具
-车
-汽车
-货车
-小较车

现在有一个车 比如宝马的一款车,是属于小较车的这个分类的
我现在想查询所有交通工具下的产品。

如何构建查询呢??? 或者如何构建表可以解决这个问题呢?
请各位兄弟提提意思。。
...全文
209 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sprrav 2005-02-26
  • 打赏
  • 举报
回复
表结构这样:key,parentKey,name,然后创建一个函数可自动生成一个临时表,该临时表包含
key,parentKey,name,还有path.其中path为name1/name2/name3/这样的结构。
要查询所有name1的子,只要select * from Argu() where name like 'name1%'
NewQger 2005-02-25
  • 打赏
  • 举报
回复
续,补漏字 level 字段可以不用
查询时截取id的相应位长分组
NewQger 2005-02-25
  • 打赏
  • 举报
回复
结构:表(id,preid,level,...)
使用时id使用上级id的编号+本级的编号(level 字段可以,需要定好每级编号的位数)
JafyLiu 2005-02-25
  • 打赏
  • 举报
回复
在最末端的叶子节点加一标识,以识别此节点为最末端的节点而不是某一级分类节点,这样不管从哪一级开始检索都能保证检出的结果是实际的内容而不是分类,这种方法对统计和报表的唯一和正确性应该是很有帮助的。
631799 2005-02-25
  • 打赏
  • 举报
回复
--邹建 2003.9--*/
create table tb(id int identity(1,1) not null constraint PK_tb primary key clustered
,pid int,name varchar(20))
insert into 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
尚和空四 2005-02-25
  • 打赏
  • 举报
回复
给各级分类有前缀编号就好办的多了
建议这样
好多oa平台就这样的
  • 打赏
  • 举报
回复
如果你的代码有序,就容易了。
jonescheng 2005-02-25
  • 打赏
  • 举报
回复
谢谢兄弟们的回复!!!
但是好象也没有什么好办法
lin_now 2005-02-25
  • 打赏
  • 举报
回复
UP
接分
zjcxc 2005-02-25
  • 打赏
  • 举报
回复
/*--示例

表结构
用户id,用户名,经理id
create table Nuser
(
userid bigint primary key,
uname varchar(50),
mangername bigint
)

如何获得一个经理的 全部不是干部的员工
当总经理时,他的 mangername=0
--*/

--查询指定id的所有子的函数
create function f_cid(
@id int
)returns @re table(userid 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.userid,@l
from Nuser a,@re b
where a.mangername=b.userid and b.[level]=@l-1
end
return
end
go

--调用(查询所有的子)
select a.*,层次=b.[level] from Nuser a,f_cid(2)b --查询 userid=2 的所有下级
where a.userid=b.userid
Softlee81307 2005-02-25
  • 打赏
  • 举报
回复
參考這個做設計吧
http://www.cnblogs.com/goodspeed/archive/2004/09/16/43714.html
FatSpiders 2005-02-25
  • 打赏
  • 举报
回复
有一个办法这样子。

id , name, frontid
1 交通工具, 0
2 车, 1
3 汽车, 2
4 货车, 3

用frontid来装上级的ID,
select * from bb where frontid=? 就可以行实树形了。
如果是同级的话就记录相同的ID就可以了。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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