title: How to select the last two records for each topic_id in MySQL
body: <p>I have to select the last two records for every topic.</p>
  
  <p>ex:
  table: msg</p>
  
  <pre><code>id  |  topic_id
  ------------
   1  |  1
   2  |  1
   3  |  1
   4  |  1
   5  |  2
   6  |  2
   7  |  2
   8  |  3
   9  |  3
  10  |  3
  </code></pre>
  
  <p>I want to obtain these rows:</p>
  
  <pre><code> 3 1
   4 1
   6 2
   7 2
   9 3
  10 3
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>thanks</p>