1

我想为我的网站访问者提供对现有评论发表评论的便利,因为我们可以在 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 值的行会发生什么,以及我将如何整理每个评论下的评论。

任何人有任何解决方案?我应该使用哪个查询以及如何整理每条评论下的评论?

4

3 回答 3

0

我不知道您是如何获得评论 sql 结果的,无论是数组还是对象,比如数组 -

$comments = array(
    array('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'),
    array('comment_id' => '1212',
        'parent_id'  => '1211',
        'thread_id'  => '122',
        'title'      => 'This is the title of the comment',
        'body'       => 'This is the Body of the comment',
        'date'       => '2013-04-04 15:05:44',
        'user'       => 'abcuser'),
);

现在对数组进行排序 -

$childs = array();
foreach ($comments as &$comment) {
    $childs[$comment['parent_id']][] = &$comment;
}
unset($comment);

foreach ($comments as &$comment) {
    if (isset($childs[$comment['comment_id']])) {
        $comment['childs'] = $childs[$comment['comment_id']];
    }
}
unset($comments);

$tree = $childs[NULL];
echo "<pre>";
print_r($tree);

输出:

Array
(
    [0] => Array
        (
            [comment_id] => 1211
            [parent_id] => 
            [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
            [childs] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 1212
                            [parent_id] => 1211
                            [thread_id] => 122
                            [title] => This is the title of the comment
                            [body] => This is the Body of the comment
                            [date] => 2013-04-04 15:05:44
                            [user] => abcuser
                        )

                )

        )

)

您也可以对对象应用相同的逻辑以按排序顺序显示结果。

于 2013-04-08T08:36:43.397 回答
0

您使用的查询SELECT * FROM comments WHERE thread_id = 122也将在评论下获得您的评论。您是否要按特定顺序获取它们?然后尝试添加order by parent_id. 或指定您希望它们的顺序。

也许您甚至可以绘制一个小型记录集,向我们展示您希望查询结果的样子。因为我不完全理解你希望他们成为什么样的人。

于 2013-04-08T08:14:20.120 回答
0
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
于 2013-04-08T08:01:58.270 回答