How to select the last two records for each topic_id in MySQL

Posted by Luca Romagnoli on Stack Overflow See other posts from Stack Overflow or by Luca Romagnoli
Published on 2010-04-07T23:01:59Z Indexed on 2010/05/14 1:24 UTC
Read the original article Hit count: 275

Filed under:
|

Hi. I have to select the last two records for every topic.

ex: table: msg

id  |  topic_id
------------
 1  |  1
 2  |  1
 3  |  1
 4  |  1
 5  |  2
 6  |  2
 7  |  2
 8  |  3
 9  |  3
10  |  3

I want to obtain these rows:

 3 1
 4 1
 6 2
 7 2
 9 3
10 3

How can I do this?

thanks

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql