2013-05-10 110 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

相关问题