title: How can I select the longest text field when using GROUP BY in mysql, a la MAX()?
body: <p>In MySql you can use the <code>MAX()</code> function to get the highest value when using <code>GROUP BY</code>, how can I do the same thing to get the longest string of text?</p>
  
  <p>Sample table:</p>
  
  <pre><code>id_|_post_id|_title__________|_body_____________________________________________
  1  | ZXBF1J | Favorite Color | My favorite color is blue.
  2  | ZXBF1J | Favorite Color | My favorite color is blue, no wait...
  3  | ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
  4  | AR3D47 | Quest          | To seek..
  5  | AR3D47 | Quest          | To seek the Holy
  6  | AR3D47 | Quest          | To seek the Holy Grail.
  </code></pre>
  
  <p>The tricky part is that I want to <code>ORDER BY id ASC</code> to see the oldest entries on the top, and I want to group by the <code>post_id</code> which is not something that I can use to <code>ORDER</code>, and get the longest <code>body</code>.</p>
  
  <p>Sample query:</p>
  
  <pre><code>SELECT post_id, title, MAX(body) // obviously MAX() doesn't work here
  FROM posts
  GROUP BY post_id
  ORDER BY id ASC
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>post_id|_title__________|_body_____________________________________________
  ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
  AR3D47 | Quest          | To seek the Holy Grail.
  </code></pre>
  
  <p>Again the key is to select the longest <code>body</code> while maintaining the order based on the <code>id</code>.</p>