【发布时间】:2009-08-11 14:53:20
【问题描述】:
我已经考虑这个问题已经有一段时间了,我需要一种方法来在数据库中添加对 cmets 的回复,但我不知道如何继续。
这是我目前的评论表(不多说但它是一个开始):
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`comment` text,
`user_id` int(12) DEFAULT NULL,
`topic_id` int(12) NOT NULL,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `topic_id` (`topic_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ;
这是我当前的查询:
SELECT c.id, c.comment, c.user_id, u.username, u.photo
FROM (comments c)
JOIN users u ON c.user_id = u.id
WHERE c.topic_id = 9
一种选择是创建一个名为“comment_replies”的新表,但我不确定是否能够在一个查询中选择所有 cmets 和评论回复,以及是否添加一个名为“reply”的新列我不知道如何对它们进行排序以在每个回复中获得每条评论。
我很想就如何处理这个问题获得一些建议。
编辑:
按照以下关于添加 parent_comment_id 的答案从 1 条评论和 2 条回复中产生这种数组:
array(2) {
[0]=>
object(stdClass)#17 (7) {
["id"]=>
string(2) "26"
["comment"]=>
string(36) "adding a comment from the admin page"
["user_id"]=>
string(2) "16"
["ts"]=>
string(10) "1249869350"
["username"]=>
string(5) "Admin"
["photo"]=>
string(13) "gravatar2.png"
["reply"]=>
string(23) "There is no admin page!"
}
[1]=>
object(stdClass)#18 (7) {
["id"]=>
string(2) "26"
["comment"]=>
string(36) "adding a comment from the admin page"
["user_id"]=>
string(2) "16"
["ts"]=>
string(10) "1249869350"
["username"]=>
string(5) "Admin"
["photo"]=>
string(13) "gravatar2.png"
["reply"]=>
string(13) "Yes there is!"
}
}
我应该如何处理这个数组以使用它,是否可以将评论与回复分开?
【问题讨论】: