2013-05-10 50 views
1

我已經看了這麼多的博客和鏈接保存分層數據在mysql數據庫一樣組嵌套模式 * 傳遞Clousure莫代爾 * 兒童家長Hierchy。但我有點混淆可以任何機構,請告訴我什麼是最好的方法來存儲分層的多個根。在Mysql中爲多個根存儲Hierchical數據的最佳方法是什麼?

e.g 
Root1 
| 
|---Child 1 
| |--Child 1 of 1 
| |--Child 2 of 2 
| 
Root 2 
|  
|--Child 2 
| |--Child 1 of 2 
| |--Child 2 of 2 

感謝adavance :)

回答

0

當你使用一個表來存儲層次結構,層次結構中的每個對象都需要父母。所以你的節點可能有這些列:

nodeid int not null not zero    the id of the node in this row 
parentid int not null, but can be zero  the id the node's parent 
nodename varchar       the node's name 
etc etc.          other attributes of the node 

有了這張表佈局的任何父無母的節點(即,與parentid = 0任何節點)是一個根節點。您的表格中可以包含儘可能多的應用程序。

您顯示的例子可能是這樣表示:

nodeid parentid nodename 
------ -------- -------- 
1  0   Root1 
2  1   Child 1 
3  2   Child 1 of 1 
4  2   Child 2 of 1 
5  0   Root2 
6  5   Child 2 
7  6   Child 1 of 2 
8  6   Child 2 of 2 
+0

我知道這是最酷的方法之一,但我正在尋找好的選擇,因爲這是很難做到的就像找到葉節點的一些操作,尋找父etd爲第N級樹。 我真的很感謝你的回答。 – 2013-05-10 18:50:06

相關問題