获取每篇帖子的最新评论
SELECT *
FROM ( `comments` )
LEFT JOIN `posts`
ON `posts`.`id` = `comments`.`case_id`
WHERE `comments`.`user_id` = '8'
GROUP BY `comments`.`case_id`
ORDER BY `comments`.`created_date` DESC
答案 0 :(得分:2)
试试这个并告诉我它是否回答了你的问题:
SELECT * FROM comments
RIGHT JOIN posts ON posts.id = comments.case_id
WHERE comments.user_id = '8' AND comments.created_date = (SELECT MAX(created_date) FROM comments WHERE case_id = posts.id)
答案 1 :(得分:0)
为什么不获取属于该用户的帖子ID列表:
SELECT posts.id
FROM posts
INNER JOIN comments ON posts.id = comments.case_id
WHERE comments.user_id = 8
然后foreach($IDs as $id)
:
SELECT * FROM comments
WHERE case_id = ${id}
ORDER BY created_date DESC
LIMIT 1
原始答案:
您只是错过了LIMIT
子句:
SELECT * from comments
LEFT JOIN posts ON posts.id = comments.case_id
WHERE comments.user_id = 8
GROUP BY comments.case_id
ORDER BY comments.created_date DESC
LIMIT 1
答案 2 :(得分:0)
SELECT
*
FROM
posts
LEFT JOIN comments ON comments.case_id = posts.post_id
WHERE
comments.comments_id IN (
SELECT
MAX(comments_id)
FROM
comments
GROUP BY
case_id
)
AND comments.user_id = '8'
希望有所帮助