I have a MySQL table where I would like to get the latest posts by "post_date" from a set of users in a particular group (they can belong to more than one group). I first get all users in the group. Then I try:
SELECT *, max(post_date) FROM posts
WHERE user_id IN ($matches)
GROUP BY user_id
ORDER BY post_date DESC
But this doesn't work. How do I solve it?
To get the latest posts from group of users, you can do so by using a self join
SELECT p.*
FROM posts p
JOIN(
SELECT user_id , max(post_date) post_date
FROM posts
GROUP BY user_id
) pp
USING(user_id,post_date)
WHERE pp.user_id IN ($matches) /* i assume here you have user ids like IN (1,2,3)*/
ORDER BY p.post_date DESC
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments