gpt4 book ai didi

php - php中如何对评论的评论进行整理

转载 作者:行者123 更新时间:2023-11-30 23:20:09 25 4
gpt4 key购买 nike

我想为我的网站访问者提供对现有评论发表评论的便利,因为我们可以对 stackoverflow 中的答案发表评论。

这里我创建了两个表

threads
thread_id (auto increment),
title VARCHAR(MAX),
body VARCHAR(MAX),
date datetime,
user INT

comments
comment_id INT (auto increment),
parent_id INT,
thread_id INT,
title VARCHAR(MAX),
body VARCHAR(MAX),
date datetime,
user INT

当用户在一个话题上发表评论时,它会被保存在评论表中,如下所示

comment_id = 1211
parent_id = NULL
thread_id = 122
title = "This is the title of the comment"
body = "This is the Body of the comment"
date = 2013-04-04 13:05:44
user = "xyzuser"

假设用户对上述评论发表评论,该评论将按如下方式保存在评论表中

comment_id = 1212
parent_id = 1211
thread_id = 122
title = "This is sample title of comment on comment"
body = "This is the Body of comment on comment";
date = 2013-04-04 15:05:44
user = "abcuser"

我正在使用以下查询从线程表中获取线程

SELECT * FROM threads WHERE thread_id = 122

到目前为止,我从下面的评论表中获取线程下的评论

SELECT * FROM comments WHERE thread_id = 122

但是现在,我还想在每条评论下显示评论,我试过以下

SELECT * FROM comments WHERE thread_id = 122 GROUP BY parent_id

但是使用此查询,parent_id 中具有 NULL 值的行会发生什么情况,以及我将如何整理每条评论下的评论。

谁有解决办法??我应该使用哪个查询以及如何整理每个评论下的评论??

最佳答案

SELECT c.* FROM comments c
LEFT JOIN
comments s
ON
s.parent_id = c.comment_id
WHERE c.thread_id = 122
GROUP BY c.comment_id
ORDER BY c.date ASC

关于php - php中如何对评论的评论进行整理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15873959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com