title: MySQL: Get latest message grouped by recipient's mobile number
body: <p>I have a series of SMS messages I got from a dump and am planning on arranging it so each row only displays the latest message in each thread. I'm having problems writing this query but essentially since the Sender is always the same number (<code>gateway number</code>) then it would be best to group it by the Recipient (<code>groupby number</code>).</p>
  
  <p>I can imagine this like email where the latest message being displayed can either be from the Sender or the Recipient (whichever is latest) but nonetheless grouped by the Recipient. Honestly, I don't know how to go about this.</p>
  
  <p><strong>Messages table.</strong> Type <code>out</code> means <code>gateway</code> sent it, <code>in</code> means <code>groupby</code> sent it.</p>
  
  <pre><code>|  id  |  groupby  |  gateway  |  message    |  type  |  created              |
  -------------------------------------------------------------------------------
  |  1   |  +111     |  +789     |  Hello      |  out   |  2015-01-01 00:00:00  |
  |  2   |  +222     |  +789     |  World      |  out   |  2015-01-02 00:00:00  |
  |  3   |  +111     |  +789     |  What's     |  in    |  2015-01-03 00:00:00  |
  |  4   |  +222     |  +789     |  New        |  in    |  2015-01-04 00:00:00  |
  |  5   |  +111     |  +789     |  With You?  |  out   |  2015-01-05 00:00:00  |
  -------------------------------------------------------------------------------
  </code></pre>
  
  <p>So the result should be:</p>
  
  <p><strong>Result in html.</strong></p>
  
  <pre><code>|  id  |  groupby  |  message    |  sent from  |
  ------------------------------------------------
  |  5   |  +111     |  With You?  |  +789       |
  |  4   |  +222     |  New        |  +222       |
  ------------------------------------------------
  </code></pre>