MSSQL递归问题

Se_先森_ 2017-06-29 07:20:00
ID Name ParentID
1 张三 0
2 李四 1
3 王五 2

以上表结构,现在知道一个ID为3, 需要查询出来的结果是这样的
ID Result ParentID
1 张三 0
2 张三/李四 1
3 张三/李四/王五 2

就是把名字像目录结构一样拼接起来, 不知道怎么查询, 如果SQL不能实现, 我就在代码里面写递归了, 求大神
...全文
153 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2017-06-29
  • 打赏
  • 举报
回复
引用 3 楼 u011665712 的回复:
有个小问题, 我现在知道我的ID是3的话, 怎么查出这样的效果呢
-- 反过来写,就可以了 create table test(ID int, Name varchar(10), ParentID int) go insert into test values(1, '张三', 0),(2, '李四', 1),(3, '王五', 2) go with m as ( select t.ID, cast(t.name as nvarchar(30)) name, t.ParentID from test t where t.ID = 3 union all select t.ID, cast(t.Name + '/' + m.Name as nvarchar(30)), t.ParentID from test t, m where t.ID = m.ParentID ) select * from m -- where ParentId = 0 ,如果只要自己的这条,加这个条件,全要就不加 go drop table test go (3 行受影响) ID name ParentID ----------- ------------------------------ ----------- 3 王五 2 2 李四/王五 1 1 张三/李四/王五 0 (3 行受影响)
Se_先森_ 2017-06-29
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

create table test(ID int, Name varchar(10), ParentID int)
go
insert into test values(1, '张三', 0),(2, '李四', 1),(3, '王五', 2)
go
with m as (
	select t.ID, cast(t.name as nvarchar(30)) name, t.ParentID 
	from test t where t.ParentID = 0
	union all
	select t.ID, cast(m.Name + '/' + t.Name as nvarchar(30)), t.ParentID 
	from test t, m where t.ParentID = m.ID
)
select * from m
go
drop table test
go


(3 行受影响)
ID          name                           ParentID
----------- ------------------------------ -----------
1           张三                           0
2           张三/李四                      1
3           张三/李四/王五                 2

(3 行受影响)


有个小问题, 我现在知道我的ID是3的话, 怎么查出这样的效果呢
Se_先森_ 2017-06-29
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

create table test(ID int, Name varchar(10), ParentID int)
go
insert into test values(1, '张三', 0),(2, '李四', 1),(3, '王五', 2)
go
with m as (
	select t.ID, cast(t.name as nvarchar(30)) name, t.ParentID 
	from test t where t.ParentID = 0
	union all
	select t.ID, cast(m.Name + '/' + t.Name as nvarchar(30)), t.ParentID 
	from test t, m where t.ParentID = m.ID
)
select * from m
go
drop table test
go


(3 行受影响)
ID          name                           ParentID
----------- ------------------------------ -----------
1           张三                           0
2           张三/李四                      1
3           张三/李四/王五                 2

(3 行受影响)


完美解决, 谢谢大大
卖水果的net 2017-06-29
  • 打赏
  • 举报
回复

create table test(ID int, Name varchar(10), ParentID int)
go
insert into test values(1, '张三', 0),(2, '李四', 1),(3, '王五', 2)
go
with m as (
	select t.ID, cast(t.name as nvarchar(30)) name, t.ParentID 
	from test t where t.ParentID = 0
	union all
	select t.ID, cast(m.Name + '/' + t.Name as nvarchar(30)), t.ParentID 
	from test t, m where t.ParentID = m.ID
)
select * from m
go
drop table test
go


(3 行受影响)
ID          name                           ParentID
----------- ------------------------------ -----------
1           张三                           0
2           张三/李四                      1
3           张三/李四/王五                 2

(3 行受影响)


22,209

社区成员

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

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