在樹型結構中如何從資料庫中查詢並顯示子節點

時間 2022-02-01 21:10:14

1樓:匿名使用者

先根據父編碼排序,父編碼相同的情況下再根據子編碼排序

select id,name

from [table1]

order by isnull(sjbm,0)+'.',id

不確定你的sjbm是什麼型別的,假如是int型的話,就要判斷為null值的情況,對你給的資料分析,頂級資料的sjbm值可能是空的

產生的結果應該是

1 a2 b 1

4 d 1

3 c 2

5 e 4

6 f 4

但是這樣只能實現二級項排序,對於你現有的資料,應該是3級項,可以實現,但如果是n級項的話,就只能在儲存過程裡實現了

以下是3級項排序的實現方法,其中的排序值就是各項id,以「.」分隔:

--1級項,sjbm為空,排序值為(x)

select id,name ,id orderid

from [table1]

where sjbm is null

union all

--2級項,sjbm與1級項的id相等,排序值為(x.y)

select id,name ,[lv1].id + '.' + [lv2].id

from [table1] as [lv2]

inner join

( select id

from [table1]

where sjbm is null

) as [lv1] on [lv2].sjbm = [lv1].id

union all

--3級項,sjbm與2級項的id相等,排序值為(x.y.z)

select id,name ,[lv2].sjbm + '.' + [lv2].id + '.' + [lv3].id

from [table1] as [lv3]

inner join

( select id,name,sjbm

from [table1]

where sjbm in

( select id

from [table1]

where sjbm is null

) as [lv2] on [lv3].sjbm=[lv2].id

order by orderid

如果顯示orderid的話,應該如下結果

1 a 1

2 b 1.2

3 c 1.2.3

4 d 1.4

5 e 1.4.5

6 f 1.4.6

2樓:匿名使用者

你是想從資料庫中查出來一個樹呢,還是什麼別的?

j**a從資料庫中查詢資料,儲存為樹形結構,怎麼做

mysql 查詢樹形結構資料,查詢某個節點下的所有子節點資料。

怎麼將資料庫中存的樹轉化為樹形列表

j**a樹形結構如何從資料庫讀取資料

3樓:

定義一個treenode類.裡面有

id 自身id

parentid 父節點id

name 樹顯示文字

url 連結地址

open 是否樹

等屬性.

將從資料庫查出來的資料一個個set到treenode裡面.封裝成一個list.

再將這個list轉換成你的樹所需要的格式內容.比如json格式

4樓:

select * from xx where 1=1 and( parentid=id or id=id)

如何查詢oracle資料庫中已經建立的索引

根據表名,查詢一張表的索引 select from user indexes where table name upper 表名 根據索引號,查詢表索引欄位 select from user ind columns where index name 索引名 根據索引名,查詢建立索引的語句 selec...

php中查詢資料庫的selectin語句

這麼寫當然不行,因為in裡面是個字串你放陣列格式就不對 用implode array 把陣列轉為字串就行,不用迴圈。php裡面也有where in 不可以寫陣列,要吧陣列轉換成字串 implode array abc 需要拼接為字串 arr array 1 2 3 4 5 abc implode a...

mysql中同時查詢兩個資料庫中的資料

端木怡雍謐 mysql中,可用庫名字首同時查詢兩個資料庫中的資料。工具 mysql 5.6步驟 1 如圖可見,在本地localhost中有2個資料庫,分別是badkano和badkano test。2 假如兩個資料庫中有相同的表student,資料分別如下 3 要同時查詢兩個資料庫中的上邊兩張表,語...