佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 三種實現(xiàn)方法實現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點

三種實現(xiàn)方法實現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點

熱門標(biāo)簽:地方門戶網(wǎng)站 鐵路電話系統(tǒng) 服務(wù)外包 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 百度競價排名 AI電銷
示例問題如下: 
表結(jié)構(gòu):  
Id ParentId 
1 0 
2 1 
3 2 
......  

針對該表結(jié)構(gòu)解釋如下: 
1的父節(jié)點為0, 
2的父節(jié)點為1, 
3的父節(jié)點為2 
...... 


以此類推,要求給定一個父節(jié)點的值,比如1, 


用SQL語句查詢的到該父結(jié)點下的所有子節(jié)點 

 

下面的Sql是在Sql Server下調(diào)試通過的,如果是Oracle,則有Connect By可以實現(xiàn). 


建立測試表: 

 

Drop Table DbTree 

Create Table DbTree 



[Id] Int, 

[Name] NVarChar(20), 

[ParentId] Int 



 


插入測試數(shù)據(jù): 

 

Insert Into DbTree ([Id],[ParentId]) Values (1,0) 

Insert Into DbTree ([Id],[ParentId]) Values (2,1) 

Insert Into DbTree ([Id],[ParentId]) Values (3,1) 

Insert Into DbTree ([Id],[ParentId]) Values (4,3) 

Insert Into DbTree ([Id],[ParentId]) Values (5,4) 

Insert Into DbTree ([Id],[ParentId]) Values (6,7) 

Insert Into DbTree ([Id],[ParentId]) Values (8,5) 

 

實現(xiàn)方法一: 


代碼如下: 

 

Declare @Id Int 

Set @Id = 1 ---在次修改父節(jié)點 

Select * Into #Temp From DbTree Where ParentId In (@Id) 

Select * Into #AllRow From DbTree Where ParentId In (@Id) --1,2 


While Exists(Select * From #Temp) 

Begin 

Select * Into #Temp2 From #Temp 

Truncate Table #Temp 


Insert Into #Temp Select * From DbTree Where ParentId In (Select Id From #Temp2) 

Insert Into #AllRow Select * From #Temp 

Drop Table #Temp2 

End 

Select * From #AllRow Order By Id 


Drop Table #Temp 

Drop Table #AllRow 

 

 


實現(xiàn)方法二: 


代碼如下: 

 

Create Table #AllRow 



Id Int, 

ParentId Int 




Declare @Id Int 

Set @Id = 1 ---在次修改父節(jié)點 


Delete #AllRow 


--頂層自身 

Insert Into #AllRow (Id,ParentId) Select @Id, @Id 


While @@RowCount > 0 

Begin 

Insert Into #AllRow (Id,ParentId) 

Select B.Id,A.Id 

From #AllRow A,DbTree B 

Where A.Id = B.ParentId And 

Not Exists (Select Id From #AllRow Where Id = B.Id And ParentId = A.Id) 

End 


Delete From #AllRow Where Id = @Id 

Select * From #AllRow Order By Id 

Drop Table #AllRow 

 


實現(xiàn)方法三: 


代碼如下: 

 

在Sql Server2005中其實提供了CTE[公共表表達(dá)式]來實現(xiàn)遞歸: 

關(guān)于CTE的使用請查MSDN 

Declare @Id Int 

Set @Id = 3; ---在次修改父節(jié)點 


With RootNodeCTE(Id,ParentId) 

As 



Select Id,ParentId From DbTree Where ParentId In (@Id) 

Union All 

Select DbTree.Id,DbTree.ParentId From RootNodeCTE 

Inner Join DbTree 

On RootNodeCTE.Id = DbTree.ParentId 




Select * From RootNodeCTE 

標(biāo)簽:仙桃 蘭州 湘潭 銅川 崇左 湖南 黃山 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《三種實現(xiàn)方法實現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    巩留县| 正安县| 河北省| 武宣县| 高清| 宁武县| 泰来县| 恩施市| 莱阳市| 越西县| 苏尼特右旗| 东山县| 昌图县| 云林县| 浪卡子县| 平原县| 延安市| 定结县| 东莞市| 滨海县| 凤庆县| 肇州县| 久治县| 溧水县| 瑞安市| 东安县| 兰州市| 邢台县| 滨州市| 准格尔旗| 金昌市| 南昌市| 昆山市| 正定县| 凤凰县| 铜陵市| 昌宁县| 涡阳县| 襄垣县| 荥经县| 商丘市|