Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Selecting the most recent post by each of the 10 most recent authors

Tags:

mysql

I have a table containing blog posts by many different authors. What I'd like to do is show the most recent post by each of the 10 most recent authors.

Each author's posts are simply added to the table in order, which means there could be runs of posts by a single author. I'm having a heck of time coming up with a single query to do this.

This gives me the last 10 unique author IDs; can it be used as a sub-select to grab the most recent post by each author?

SELECT DISTINCT userid
FROM posts
ORDER BY postid DESC 
LIMIT 10
like image 540
Charles Johnson Avatar asked Apr 16 '12 21:04

Charles Johnson


1 Answers

select userid,postid, win from posts where postid in (
SELECT max(postid) as postid
FROM posts 
GROUP BY userid
) 
ORDER BY postid desc 
limit 10

http://sqlfiddle.com/#!2/09e25/1

like image 99
Nesim Razon Avatar answered Oct 02 '22 19:10

Nesim Razon