Estimated posts are appointed in ascending order:
SELECT max(post_id) AS post_id FROM posts GROUP BY user_id;
If such an assumption cannot be made, you can do something like:
SELECT user_id, max(date) AS most_recent FROM posts GROUP BY user_id
you can use this to capture the full post:
SELECT p1.* FROM (SELECT max(post_id) AS post_id FROM posts GROUP BY user_id) p2 JOIN p1 ON (p1.post_id = p2.post_id);
or for the absence of an assumption:
SELECT p1.* FROM (SELECT user_id, max(date) AS most_recent FROM posts GROUP BY user_id) p2 JOIN p1 ON (p1.user_id = p2.user_id AND p2.most_recent = p1.date);
You will receive several messages from the user if the latest messages have the same date. Perhaps this is correct, since they are not newer.
Note. They can also be written as correlated subqueries. Benchmarking will tell you which ones are best suited for your particular case.
source share