求一个存储过程

苦逼de小Coder 2014-04-04 03:54:00
有这么两张表

例如要求查询id为XX下的所有子分类id与其子分类下所有信息
求帮助
...全文
118 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
苦逼de小Coder 2014-04-08
  • 打赏
  • 举报
回复
引用 4 楼 fredrickhu 的回复:
;with f as 
(
select * from tb1 where id=xx
union all
select a.* from tb1 as a inner join f as b on a.parentid=b.id
)
 
select * from f  as a inner join tb2 as b on a.id=b.cid
不好意思,前几天有事了,所以没有及时结贴。非常感谢
MootShao 2014-04-04
  • 打赏
  • 举报
回复
思路:使用CTE的递归 求树的上级或者下级
--小F-- 2014-04-04
  • 打赏
  • 举报
回复
;with f as 
(
select * from tb1 where id=xx
union all
select a.* from tb1 as a inner join f as b on a.parentid=b.id
)
 
select * from f  as a inner join tb2 as b on a.id=b.cid
苦逼de小Coder 2014-04-04
  • 打赏
  • 举报
回复
可能我没说明白,第一张表的id 就是 第二张表的cid
苦逼de小Coder 2014-04-04
  • 打赏
  • 举报
回复
引用 1 楼 fredrickhu 的回复:
;with f as 
(
select * from tb where id=xx
union all
select a.* from tb as a inner join f as b on a.parentid=b.id
)

select * from f
额。。关联的第二张表的count字段内容没有啊
--小F-- 2014-04-04
  • 打赏
  • 举报
回复
;with f as 
(
select * from tb where id=xx
union all
select a.* from tb as a inner join f as b on a.parentid=b.id
)

select * from f

22,209

社区成员

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

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