title: select id from table with max date and max id if dates are the same
body: <p>For an example I have MySQL table like:</p>
  
  <pre><code>id  category  date        value
  1   a         2013-01-02  7
  2   a         2013-01-01  2
  3   a         2013-01-01  3
  4   b         2013-01-01  1
  5   b         2013-01-02  4
  6   b         2013-01-03  5
  7   c         2013-01-03  4
  8   c         2013-01-03  8
  </code></pre>
  
  <p>I need select records with MAX(date) for each category, but if date is the same, with MAX(id).
  So, expected result must be:</p>
  
  <pre><code>id  category  date        value
  1   a         2013-01-02  7
  6   b         2013-01-03  5
  8   c         2013-01-03  8
  </code></pre>
  
  <p>As you can see, dates for same category can be not sorted (like id=4,5).</p>
  
  <p>I tried query like:</p>
  
  <pre><code>SELECT * FROM mytable t1
  INNER JOIN 
  (
     SELECT category, MAX(date) AS MAXDATE
     FROM mytable
     GROUP BY category
  ) t2
  ON t1.category = t2.category
  AND t1.date = t2.MAXDATE
  </code></pre>
  
  <p>It works fine, when we have no same dates in table. But for my case, it will return records with lowest ids:</p>
  
  <pre><code>   id  category  date        value
      1   a         2013-01-01  7
      6   b         2013-01-03  5
      7   c         2013-01-03  4
  </code></pre>