Last x blog entries - but only once per user

前端 未结 4 1328
攒了一身酷
攒了一身酷 2020-12-18 13:59

I would like to display a box with the last x (say 5) blog entries. But I would like to avoid that a very active user is listed twice.

My tryout boils down to:

4条回答
  •  独厮守ぢ
    2020-12-18 14:17

    Without knowing the table structure, it is rather difficult, however, this should give you a starting point:

    SELECT `User`.`id`, max(`Blog`.`published`) 
    FROM `comm_blogs` AS `Blog` 
    LEFT JOIN `comm_users` AS `User` ON (`Blog`.`user_id` = `User`.`id`) 
    WHERE `Blog`.`status` = 1 
    GROUP BY `User`.`id` 
    ORDER BY 2 DESC LIMIT 5
    

    By selecting the max publishing date per use and then sorting afterwards, you will get a list of the 5 most recent posts by different users. You will need a second query afterwards to get the contents however.

提交回复
热议问题