php - PHP 和 MySQL 中的嵌套注释

标签 php mysql

我在论坛上搜索过,但没有得到权威的答案。我想以这样的方式实现嵌套注释结构:

<ul>
    <li>This is the parent first comment!
        <ul>
            <li>This is the reply for the first parent comment!
                <ul>
                    <li>This is a reply for the first reply of the parent comment!</li>
                    <li>This is a third reply for the first parent comment!</li>
                </ul>
            </li>
            <li>This is another reply for first parent comment!</li>
        </ul>
    </li>
    <li>This is gonna be parent second comment!
        <ul>
            <li>This is a reply for the second comment!</li>
        </ul>
    </li>
    <li>This is fourth parent comment!</li>
</ul>

我的表的转储如下:

+----+------------------------------------------------------------+--------+
| id | text                                                       | parent |
+----+------------------------------------------------------------+--------+
|  1 | This is the parent first comment!                          |      0 |
|  2 | This is gonna be parent second comment!                    |      0 |
|  3 | This is the reply for the first parent comment!            |      1 |
|  4 | This is another reply for first parent comment!            |      1 |
|  5 | This is a reply for the first reply of the parent comment! |      3 |
|  6 | This is a reply for the second comment!                    |      2 |
|  7 | This is a third reply for the first parent comment!        |      3 |
|  8 | This is fourth parent comment!                             |      0 |
+----+------------------------------------------------------------+--------+

我知道如何使用mysql_query()while()循环。 PHP 和 MySQL 的初学者。请帮帮我。

最佳答案

有几种不同的方法可以在 MySQL 中存储层次结构数据。查看 Bill Karwin 的 presentation这演示了四个选项。

  • 邻接列表
  • 路径枚举
  • 嵌套集
  • 关闭表
<小时/>

您正在使用邻接列表模型来存储层次结构数据,但不幸的是,这是您可以选择用于查询子树的最难模型。

nested sets query subtree

您的选择是:

  • 更改为不同的型号。
  • 将查询限制为 n 层深度。
  • 使用存储过程进行递归查询。有关这方面的更多信息,请参阅 Quassnoi 的系列文章 - Hierarchical queries in MySQL .

关于php - PHP 和 MySQL 中的嵌套注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872573/

相关文章:

web-hosting - max_user_connections 或超出 ip 连接限制

php - 如何获得两个数组的关键交集?

mysql - 使用 3 个表选择查询

php - Codeigniter 安全删除数据库条目

php - Magento 删除数据库

php - 编码为 Utf8,我有一些

MYSQL 触发器从另一个表插入

mysql - 如何显示MySQL表中的所有记录,无论where语句如何

mysql - Mysql 中查询速度慢,性能不佳。 mysql-slow.log 中查询时间较长

MySQL 嵌套查询选择语句