title: Mysql Group By and Order by giving strange results
body: <p>Im developing a chat tool and im having problems with the sql returning the right results with group by and order by. The table has a structure shown below;</p>
  
  <p>tablename: chat</p>
  
  <pre><code>id | from | to | sent | read | message
  </code></pre>
  
  <p>'from' and 'to' are a signed integers (userids)
  'sent' is a timestamp when the message was sent.
  'message' is text msg
  'read' is an int (0 for unread, 1 for read).</p>
  
  <p>im trying to return a list of the most recent messages grouped by users</p>
  
  <p>eg</p>
  
  <pre><code>id         from     to      message        sent    read
  7324       21      1       try again    1349697192  1
  7325       251       1     yo whats up  1349741502  0
  7326       251       1     u there      1349741686  0   
  </code></pre>
  
  <p>should return this after the query</p>
  
  <pre><code>    id      from    to     message      sent        read
  7326        251     1        u there    1349741686   0
  7324        21      1       try again    1349697192  1
  </code></pre>
  
  <p>here is my query</p>
  
  <pre><code>$q ="SELECT chat.to,chat.read,chat.message,chat.sent,chat.from FROM `chat` WHERE chat.to=$userid GROUP BY chat.from ORDER BY chat.sent DESC,chat.read ASC LIMIT ".(($page-1)*$count).",$count";            
  </code></pre>
  
  <p>it doesnt return the desired results;</p>