如何查询子类别数据

JasonZhou 2010-03-30 11:52:32
有两个表
ArticleCategory[文章类别表(无限级)]
字段:CategoryId,ParentId,CategoryName


Article[文章内容表]
字段:ArticleId,CategoryId,Title,Content

我现在想要查询Article表中一个类别、及类别下所有子类别有文章

这个存储过程要怎么写?
...全文
38 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2010-03-30
  • 打赏
  • 举报
回复
with t as
(
select a.CategoryName,b.Title,b.Content
from ArticleCategory a
join Article b on a.CategoryId=b.CategoryId
where a.CategoryName='你提供的类别名'
union all
select a.CategoryName,b.Title,b.Content
from ArticleCategory a
join Article b on a.CategoryId=b.CategoryId
join t c on a.ParentId=c.CategoryId
)
select * from t
东那个升 2010-03-30
  • 打赏
  • 举报
回复
create proc sp_name (@CategoryId  int)

as

with cte as(
select a.CategoryName,b.*,0 as levl from ArticleCategory a, Article b
where a.CategoryId=b.@CategoryId
union all
select a.CategoryName,b.*, levl+1 from ArticleCategory a, Article b, cte c
where a.CategoryId=b.CategoryId and a.ParentId=c.CategoryId
)

select * from cte

27,579

社区成员

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

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