sql server: Parent/Child hierarchy tree view
sql server - Parent/Child hierarchy tree view - Stack Overflow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | --- declare @pc table (CHILD_ID int , PARENT_ID int , [ NAME ] varchar (80)); insert into @pc select 1, NULL , 'Bill' union all select 2,1, 'Jane' union all select 3,1, 'Steve' union all select 4,2, 'Ben' union all select 5,3, 'Andrew' union all select 6, NULL , 'Tom' union all select 7,8, 'Dick' union all select 8,6, 'Harry' union all select 9,3, 'Stu' union all select 10,7, 'Joe' ; ; with r as ( select CHILD_ID, PARENT_ID, [ NAME ], depth=0, sort= cast (CHILD_ID as varchar ( max )) from @pc where PARENT_ID is null union all select pc.CHILD_ID, pc.PARENT_ID, pc.[ NAME ], depth=r.depth+1, sort=r.sort+ cast (pc.CHILD_ID as varchar (30)) from r inner join @pc pc on r.CHILD_ID=pc.PARENT_ID where r.depth<32767 ) select tree=replicate( '-' ,r.depth*3)+r.[ NAME ] from r order by sort option (maxrecursion 32767); GO --https://www.sqlteam.com/articles/more-trees-hierarchies-in-sql SELECT a.ID, a.CategoryName, COALESCE (b.CategoryName, '-' ) AS 'ParentName' FROM Category AS a LEFT JOIN Category AS b on a.ParentID = b.ID GO create table pc ( parent varchar (10), child varchar (10) ) insert into pc values ( 'a' , 'b' ); insert into pc values ( 'a' , 'c' ); insert into pc values ( 'b' , 'e' ); insert into pc values ( 'b' , 'f' ); insert into pc values ( 'a' , 'd' ); Insert into pc values ( 'b' , 'g' ); insert into pc values ( 'c' , 'h' ); insert into pc values ( 'c' , 'i' ); insert into pc values ( 'd' , 'j' ); insert into pc values ( 'f' , 'k' ); insert into pc values ( 'x' , 'y' ); insert into pc values ( 'y' , 'z' ); insert into pc values ( 'm' , 'n' ); DECLARE @parent varchar (10) = 'a' ; WITH cte AS ( select null parent, @parent child, 0 as level union SELECT a.parent, a.child , 1 as level FROM pc a WHERE a.parent = @parent UNION ALL SELECT a.parent, a.child , c. level + 1 FROM pc a JOIN cte c ON a.parent = c.child ) SELECT distinct parent, child , level FROM cte order by level , parent go -- https://stackoverflow.com/questions/9676637/sql-parent-child-tree-with-sort-order -- https://stackoverflow.com/questions/49903446/sql-parent-child-tree-data-return-only-completed-tree-nodes-from-list -- https://www.sqlteam.com/articles/more-trees-hierarchies-in-sql declare @relations table (ParentID int , ChildID int , SortOrder int , treeID int ); insert into @relations values (0,1,0,0), (1,2,1,0), (2,3,2,0), (2,4,1,0), (2,6,3,0), (1,7,2,0), (1,9,3,0), (9,10,1,0), (9,12,2,0) --tree 0 , (0,1,0,1), (1,2,1,1), (2,3,2,1), (2,4,1,1), (2,6,3,1), (1,7,2,1), (1,9,3,1), (9,10,1,1), (9,12,2,1) --tree 1 ; with cte(ParentId,ChildId,SortOrder,depth,agg,treeID) as ( select null ,ParentId,SortOrder,0 , right ( '0000000' + CAST (treeID as varchar ( max )),7) + right ( '0000000' + CAST (SortOrder as varchar ( max )),7) , treeID from @relations where ParentId=0 union all select cte.ChildId,r.ChildId,r.SortOrder,cte.depth+1 , cte.agg + right ( '0000000' + CAST (r.treeID as varchar ( max )),7) + right ( '0000000' + CAST (r.SortOrder as varchar ( max )),7) + right ( '0000000' + CAST (r.ChildId as varchar ( max )),7) , r.treeID from cte inner join @relations r on r.ParentID=cte.ChildId where cte.depth<32767 and r.treeID=cte.treeID ) select tree= case depth when 1 then cast (ParentID as varchar (30))+ ' (sort ' + cast (SortOrder as varchar (30))+ ')' else REPLICATE( CHAR (9),depth-1) + cast (ChildId as varchar (30))+ ' (sort ' + cast (SortOrder as varchar (30))+ ')' end from cte where depth>0 order by agg option (maxrecursion 32767); go ---顺序排列Geovin Du PageSet,CategoryIsDisplay,CategoryPojectId WITH cte AS ( SELECT ID, [ParentID], CategoryName, PageSet, IdOrder, CAST (0 AS varbinary( max )) AS Level FROM Category WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1 UNION ALL SELECT i.ID, i.[ParentID], i.CategoryName, i.PageSet, i.IdOrder, Level + CAST (i.ID AS varbinary( max )) AS Level FROM Category i INNER JOIN cte c ON c.ID = i.[ParentID] ) SELECT ID, [ParentID], CategoryName, PageSet, IdOrder FROM cte ORDER BY [ Level ]; go |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | WITH cte AS ( SELECT ID, [ParentID], CategoryName, PageSet, IdOrder, dbo.f_getlevel(ID) AS Dulv, CAST (0 AS varbinary( max )) AS Level FROM Category WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1 UNION ALL SELECT i.ID, i.[ParentID], i.CategoryName, i.PageSet, i.IdOrder, dbo.f_getlevel(i.ID) AS Dulv, Level + CAST (i.ID AS varbinary( max )) AS Level FROM Category i INNER JOIN cte c ON c.ID = i.[ParentID] ) SELECT ID, [ParentID], CategoryName, PageSet, IdOrder, Dulv FROM cte ORDER BY [ Level ]; go -- WITH cte AS ( SELECT ID, [ParentID], CategoryName, PageSet, IdOrder, dbo.f_getlevel(ID) AS Level FROM Category WHERE [ParentID] is null and CategoryIsDisplay=1 and CategoryPojectId=1 UNION ALL SELECT i.ID, i.[ParentID], i.CategoryName, i.PageSet, i.IdOrder, dbo.f_getlevel(i.ID) AS Level FROM Category i INNER JOIN cte c ON c.ID = i.[ParentID] ) SELECT ID, [ParentID], CategoryName, PageSet, IdOrder, Level FROM cte ORDER BY Level ; go |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
数据库编程
标签:
tree
, sql server
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2016-09-18 csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
2015-09-18 sql:Oracle11g 表,视图,存储过程结构查询
2015-09-18 sql:MySQL 6.7 表,视图,存储过程结构查询
2010-09-18 csharp:DataRelation 对象访问相关数据表中的记录