sql语句 面试题

cooolchen 2008-06-11 12:14:03
有一个分类表category

categoryID,categoryName,parentID

现在要根据categoryID 得到它的分类路径,这个语句怎么写?
...全文
237 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Maggie 2008-06-25
  • 打赏
  • 举报
回复
select *, CONVERT (varchar(MAX),PName ) AS Path from Test
where ParentID = 45
改动了下
这个 第 57 行: 'MAX' 附近有语法错误。
很慌!
Garnett_KG 2008-06-23
  • 打赏
  • 举报
回复
if object_id('tempdb..#t') is not null drop table #t
create table #t(categoid int,categoname varchar(10),parentid int)
insert into #t
select 8,'2222',0
union all
select 9,'2345',8
union all
select 12,'we32',9

;
with cte
as (
select categoname,parentid,1 as lvl,cast(categoname as varchar(8000)) as paths
from #t
where categoid=12

union all
select a.categoname,a.parentid,b.lvl+1,a.categoname+'\'+b.paths
from #t a join cte b
on a.categoid=b.parentid
)
select * from cte where lvl=(select max(lvl) from cte)
ojuju10 2008-06-23
  • 打赏
  • 举报
回复

参考下:
http://topic.csdn.net/u/20080620/10/44224547-2a51-4e0b-a721-132813bae468.html
cooolchen 2008-06-22
  • 打赏
  • 举报
回复
回帖是一种美德!传说每天回帖即可获得 10 分可用分!
lff642 2008-06-11
  • 打赏
  • 举报
回复
难道是根据categoryID找上级parentID.
hanjs 2008-06-11
  • 打赏
  • 举报
回复
给出测试数据及你要的结果,,
arrow_gx 2008-06-11
  • 打赏
  • 举报
回复
分类路径 是根据 categoryID parentID 来组合成的 ??

select 'd:\aaa\bbb\ccc\'+cast(parentID  as varchar(10))+ cast(categoryID as varchar(10)) from category where categoryID=100
arrow_gx 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 cooolchen 的帖子:]
有一个分类表category

categoryID,categoryName,parentID

现在要根据categoryID 得到它的分类路径,这个语句怎么写?
[/Quote]

分类路径 是那个字段, 或者是根据 categoryID 来组合成的呢 ??
cooolchen 2008-06-11
  • 打赏
  • 举报
回复
catelogID catalogName parentID

8 2222 0
9 2345 8
12 we34 9

id为12的路径就是:2222\2345\we34
id为9的路径就是:2222\2345

如何用Sql语句得到这个结果。

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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