【问题标题】:What is best way to store hierchical data in Mysql for more than one root?在 Mysql 中为多个根存储分层数据的最佳方法是什么?
【发布时间】:2013-05-11 02:16:56
【问题描述】:

我已经阅读了很多博客和链接以将分层数据保存在 mysql 数据库中,例如 nested set modal *Transitive Clousure Modal* Child Parent Hierchy强>。但我有点困惑,任何人都可以建议我为多个根存储分层结构的最佳方法。

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

提前感谢:)

【问题讨论】:

    标签: mysql hierarchical-data nested-set-model


    【解决方案1】:

    当您使用表来存储层次结构时,层次结构中的每个对象都需要一个父对象。所以你的节点可能有这些列:

     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
    

    【讨论】:

    • 我知道这是最酷的方法之一,但我正在寻找好的替代方法,因为很难执行一些操作,例如查找叶节点、查找 N 级树的父 etd。我真的很感谢你的回答。
    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2010-11-25
    • 2012-08-15
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多