我有一个评论表,例如
id | post_id | parent_id | user_id | text | time | likes
评论可以有 1 级子评论(无 2/3 级嵌套)。用户可以根据时间/喜欢对评论进行排序。
获取最近 10 条评论以及所有子评论的查询是什么。
SELECT `id`, `user_id`, `text`
FROM `comments`
WHERE `post_id` = '$postId' OR `parent_id` IN
(
SELECT `id` FROM `comments`
WHERE `post_id` = '$postId'
ORDER BY `time` DESC
LIMIT 10
)
ORDER BY `time` DESC
LIMIT 10
这不起作用。
最佳答案
根据需要添加其他条件。
SELECT c.`id`, c.`parent_id`, c.`user_id`, c.`text`
FROM `comments` c,
(
SELECT `id` FROM `comments`
WHERE `post_id` = '$postId'
ORDER BY `time` DESC
LIMIT 10
) temp
where c.id = temp.id or c.parent_id=temp.id
关于php - MySql 获取最近 10 条评论以及所有子评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25240483/