我有一张桌子,人们可以发表评论:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user` int(11) UNSIGNED NOT NULL,
`reference` int(11),
`data` datetime NOT NULL,
`ip` varchar(20),
`answer` int(11)
PRIMARY KEY (`id`)
)
如果此评论是另一条评论的答案answer
有其ID。
我有这个选择:
select id, user, data, ip, answer from comments where reference = ? and answer = 0
所以我可以通过引用ID获取所有帖子评论,如果是答案则跳过它。
问题是,如果一条评论有答案,我如何在此评论中的评论中选择此答案?例如:
comment 1
answer comment 1 (1)
answer comment 1 (2)
comment 2
comment 3
comment 4
answer comment 4 (1)
answer comment 4 (2)
comment 5
...
答案 0 :(得分:0)
我想你可以
SELECT q.id as question_id , q.user as question_user,
q.data as question_data, q.ip as question_ip,
a.id as answer_id , q.user as answer_user,
a.data as answer_data, a.ip as answer_ip
FROM comments q LEFT JOIN comments a on q.id=a.answer
WHERE q.reference = ?
然后在php中,你可以遍历结果并根据你的需要组装一个数组。