如何获得每篇文章的最新评论?同时将其分组显示第一条评论记录

时间:2017-06-28 03:31:10

标签: php mysql sql database codeigniter

SELECT * FROM( comments ) LEFT JOIN posts点击postsid = commentscase_id 在哪里commentsuser_id ='8' GROUP BY commentscase_id ORDER BY commentscreated_date DESC我的编码就是这个。如何获得每篇文章的最新评论?同时将其分组显示第一条评论记录

1 个答案:

答案 0 :(得分:0)

SELECT *
FROM (  SELECT comments.*
        FROM comments 
        INNER JOIN comment2article USING (comment_id)
        WHERE comment2article.article_d= ID_of_article
    ) MAIN_TABLE 
LEFT JOIN ( SELECT comments.*
            FROM comments 
            INNER JOIN comment2article USING (comment_id)
            WHERE comment2article.article_d= ID_of_article
        ) DUMMY_TABLE ON (MAIN_TABLE.comment_id = DUMMY_TABLE.comment_id AND MAIN_TABLE.created < DUMMY_TABLE.created)
WHERE DUMMY_TABLE.comment_id IS NULL;

尝试以上查询。

希望这会对你有所帮助。