如何实现嵌套评论系统?

2022-01-07 00:00:00 nested comments jquery php mysql

实现这种事情的理想方式是什么?我现在脑子里的想法是有一个评论表,让每个评论都有一个线程标识符和父评论标识符.线程标识符将指示注释属于哪个线程,并允许使用 WHERE 子句进行简单的 MySQL 语句.根据通常的数据库设计,每个评论都有一个 auto_increment 标识符,父标识符列将指示此评论是哪个评论的子级.

What would be the ideal way to implement this sort of thing? The idea I have in my head right now is to have a comments table and have each comment have a thread identifier and parent comment identifier. The thread identifier would indicate to which thread the comment belongs to and would allow for a simple MySQL statement using the WHERE clause. Each comment would have an auto_increment identifier as per usual database design and the parent identifier column would indicate which comment this comment is a child of.

这种类型的设计会将大部分压力放在 PHP 方面,因为它只是一个 SQL 调用即可从线程中获取所有评论.我发现的另一个实现是对每个嵌套级别都有一个 SQL 查询.这种解决方案会将压力放在 SQL 方面.

This type of design would put most of the stress on the PHP aspect of things because it would only be one SQL call to get all comments from a thread. Another implementation I found was having an SQL query for each nesting level. This solution would place the stress on the SQL sides of things.

SO 将如何实现这一点?目前我不知所措,因为我不确定哪个解决方案是最佳"解决方案,而且我对数据库设计、PHP 和 JQuery 仍然很陌生.

How would SO implement this? Currently I'm at a loss because I am not sure which solution is the "best" solution and I am still quite new to database design, PHP, and JQuery.

谢谢.

推荐答案

看在 MySQL 中管理分层数据,特别是称为嵌套集模型"的部分.您可能需要通读几次才能理解它(我做到了),但这是值得的.这是一种非常强大的方式来处理嵌套数据并仅通过一个查询来检索您想要的部分.

Look at Managing Hierarchical Data in MySQL, specifically the section called "Nested Set Model". You may have to read through it a few times before it makes sense (I did) but it's worth it. It's a very powerful way to work with nested data and retrieve the parts you want with only one query.

不利的一面是,要进行更新,您必须做更多的工作.

On the downside, for updates you have to do a lot more work.

相关文章