title: php mysql Group By to get latest record, not first record
body: <p>The Table:</p>
  
  <pre><code>(`post_id`, `forum_id`, `topic_id`, `post_time`) 
  (79, 8, 4, '2012-11-19 06:58:08');
  (80, 3, 3, '2012-11-19 06:58:42'),
  (81, 9, 9, '2012-11-19 06:59:04'),
  (82, 11, 6, '2012-11-19 16:05:39'),
  (83, 9, 9, '2012-11-19 16:07:46'),
  (84, 9, 11, '2012-11-19 16:09:33'),
  </code></pre>
  
  <p>The Query:</p>
  
  <pre><code>SELECT  post_id, forum_id, topic_id FROM posts 
  GROUP BY topic_id 
  ORDER BY post_time DESC
  LIMIT 5
  </code></pre>
  
  <p>The Results:</p>
  
  <pre><code>[0] =&gt; [post_id] =&gt; 84 [forum_id] =&gt; 9 [topic_id] =&gt; 11  
  [1] =&gt; [post_id] =&gt; 82 [forum_id] =&gt; 11 [topic_id] =&gt; 6  
  [2] =&gt; [post_id] =&gt; 81 [forum_id] =&gt; 9 [topic_id] =&gt; 9  
  [3] =&gt; [post_id] =&gt; 80 [forum_id] =&gt; 3 [topic_id] =&gt; 3  
  [4] =&gt; [post_id] =&gt; 79 [forum_id] =&gt; 8 [topic_id] =&gt; 4
  </code></pre>
  
  <p>The Problem:</p>
  
  <p>How to rewrite the query so that it returns post_id -> 83 instead of post_id -> 81 ?</p>
  
  <p>They both have the same forum and topic ids, but post_id -> 81 has an older date than post_id -> 83. </p>
  
  <p>But it seems that Group By gets the 'first' record and not the 'newest' one. </p>
  
  <p>I tried changing the query to</p>
  
  <pre><code>SELECT  post_id, forum_id, topic_id, MAX(post_time)
  </code></pre>
  
  <p>but that returns both post_id 81 and 83</p>