Forum Moderators: open
I have a MySQL table that looks like this:
userId ¦ postId ¦ postDate
1 1 070101
2 2 070102
2 3 070102
1 4 070102
1 5 070103
2 6 070104
1 7 070104
2 8 070105
1 9 070107 I want a result set that looks like this:(Latest two results per user)
userId ¦ postId ¦ postDate
1 9 070107
1 7 070104
2 8 070105
2 6 070104 How can I achieve this in a single MySQL query? Is this possible?
Somthing like
SELECT userid, postid, postdate FROM table T WHERE postdate=(SELECT MAX(postdate) FROM table T2 WHERE T2.userid=T.userid)
UNION
SELECT userid, postid, postdate FROM table T where postdate=(SELECT MAX(postdate) FROM table T2 WHERE T2.userid=T.userid AND T2.postdate<>(SELECT MAX(postdate) FROM table T3 WHERE T3.userid=T1.userid))
ORDER BY userid, postid DESC