使用sqlserver遞推查詢(xún),可以直接查詢(xún)出來(lái)。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),寧江企業(yè)網(wǎng)站建設(shè),寧江品牌網(wǎng)站建設(shè),網(wǎng)站定制,寧江網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,寧江網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
參考資料:
WITH lmenu(nav_id,nav_name,nav_parentid,level) as
(
SELECT nav_id,nav_name,nav_parentid,0 level FROM nav nav_parentid=0
UNION ALL
SELECT A.nav_id, A.nav_name,a.nav_parentid, b.level+1 FROM gs_nav A,lmenu b where a.nav_parentid= b.nav_id and nav_id = 10
)
SELECT * from lmenu
具體SQL語(yǔ)句還需要自己驗(yàn)證一下,上面的自己看著亂寫(xiě)的。
看看數(shù)據(jù)表結(jié)構(gòu)
表:Tree
current_id int (當(dāng)前節(jié)點(diǎn)編號(hào))
father_id int (父節(jié)點(diǎn)編號(hào),如果是根節(jié)點(diǎn),-1)
left_id int (左節(jié)點(diǎn)編號(hào))
right_id int (右節(jié)點(diǎn)編號(hào))
表:Node
Node_id int PK
Node_vaule nvarchar(100)
說(shuō)明:Tree表是用來(lái)存儲(chǔ)樹(shù)型結(jié)構(gòu)的,Node表是用來(lái)存儲(chǔ)節(jié)點(diǎn)內(nèi)容的
其中Tree表的current_id與Node表的Node_id是一一對(duì)應(yīng)的
至于遍歷的存儲(chǔ)過(guò)程是要完成什么功能呢?
Create Table Tb
(
Pid Varchar(50),
CEID Varchar(50),
Level int,
Ccode Varchar(50),
pCode Varchar(50)
)
Insert Into TB Values('','1',0,'產(chǎn)品1','')
Insert Into TB Values('1','2',1,'Test1','產(chǎn)品1')
Insert Into TB Values('1','3',1,'Test2','產(chǎn)品1')
Insert Into TB Values('2','4',2,'Test11','Test1')
Insert Into TB Values('2','5',2,'Test12','Test1')
Insert Into TB Values('3','6',2,'Test21','Test2')
Insert Into TB Values('3','7',2,'Test22','Test2')
Insert Into TB Values('6','8',3,'Test211','Test21')
Insert Into TB Values('6','9',3,'Test212','Test21')
Insert Into TB Values('6','10',3,'Test213','Test21')
With T
As
(
Select Pid,CEID,Ccode,Level,Convert(Varchar(4000),CEID) As Path From TB ?Where Pid=''
Union All
Select TB.Pid,TB.CEID,TB.Ccode,TB.Level,Convert(Varchar(4000),T.Path+'-'+TB.CEID) From TB inner join T On TB.Pid=T.CEID
)
Select replicate(' ? ?',level)+Ccode As Ccode From T
Order By Path