设为首页 加入收藏

TOP

sql树型结构查询递归方式
2014-11-24 07:36:25 来源: 作者: 【 】 浏览:2
Tags:sql 结构 查询 方式
sql树型结构查询递归方式
表结构:tblTree(ID, ParentID, Name)
【说明】:ParentID = 0 表示 这个记录对应根目录
1)
[sql]
with SubQuery(ID,Name,ParentID) as (
select ID,Name,ParentID from tblTree where ParentID = 0
union all
select A.ID,A.Name,A.ParentID
from tblTree A inner join SubQuery B
on A.ParentID = B.ID
)
select * from SubQuery
2)
[sql]
with SubQuery(ID,Name,ParentID,[Level]) as (
select ID,Name,ParentID,0 as [Level] from tblTree where ParentID = 0
union all
select A.ID,A.Name,A.ParentID,B.Level+1 AS [Level]
from tblTree A inner join SubQuery B
on A.ParentID = B.ID
)
select * from SubQuery
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇深入浅出SQL Server 2008分区函数.. 下一篇用SQL将字符串按分隔符拆为子串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)