Fast Relational method of storing tree data (for instance threaded comments on articles)

后端 未结 6 1115
你的背包
你的背包 2020-12-13 10:59

I have a cms which stores comments against articles. These comments can be both threaded and non threaded. Although technically they are the same just with the reply column

6条回答
  •  -上瘾入骨i
    2020-12-13 11:23

    You've got a choice between the adjacency and the nested set models. The article Managing Hierarchical Data in MySQL makes for a nice introduction.

    For a theoretical discussion, see Celko's Trees and Hierarchies.

    It's rather easy to implement a threaded list if your database supports windowing functions. All you need is a recursive reference in your target database table, such as:

    create Tablename (
      RecordID integer not null default 0 auto_increment,
      ParentID integer default null references RecordID,
      ...
    )
    

    You can then use a recursive Common Table Expression to display a threaded view. An example is available here.

提交回复
热议问题