title: Returning the 'last' row of each 'group by' in MySQL
body: <p>Is there a more efficient way of doing the following?</p>
  
  <pre><code>select * 
      from foo as a
      where a.id = (select max(id) from foo where uid = a.uid group by uid)
      group by uid;
  )
  </code></pre>
  
  <p>This answer looks similar, but is this answer the best way of doing this -  <a href="https://stackoverflow.com/questions/2739474/how-to-select-the-first-row-for-each-group-in-mysql/2739528#2739528">How to select the first row for each group in MySQL?</a></p>
  
  <p>Thanks,</p>
  
  <p>Chris.</p>
  
  <p>P.S. the table looks like:</p>
  
  <pre><code>CREATE TABLE foo (
      id INT(10) NOT NULL AUTO_INCREMENT,
      uid INT(10) NOT NULL,
      value VARCHAR(50) NOT NULL,
      PRIMARY KEY (`id`),
      INDEX `uid` (`uid`)
  )
  </code></pre>
  
  <p>data:</p>
  
  <pre><code>id, uid, value
   1,   1, hello
   2,   2, cheese
   3,   2, pickle
   4,   1, world
  </code></pre>
  
  <p>results:</p>
  
  <pre><code>id, uid, value
   3,   2, pickle
   4,   1, world
  </code></pre>
  
  <p>See <a href="http://www.barricane.com/2012/02/08/mysql-select-last-matching-row.html" rel="nofollow noreferrer">http://www.barricane.com/2012/02/08/mysql-select-last-matching-row.html</a> for more details.</p>