title: php Mysql Grouping and Ordering user messages together
body: <p>PHP Mysql Table:Messages</p>
  
  <pre><code>id   sender   receiver      time_sent          Message  SendDel RecDel
  1     1          3      2011-08-17 14:00:00     [text]     0      0
  2     3          1      2011-08-17 15:00:00     [text]     0      0
  3     2          4      2011-08-18 14:19:28     [text]     1      0
  4     4          2      2011-08-18 15:19:28     [text]     0      0
  </code></pre>
  
  <p>Objective is to retrieve the highest value message(MAX) and group the messages based on sender,receiver. So message id's 1 and 2 would group together and id's 3 and 4 would group together.</p>
  
  <p>Example: logged in userid = 2 Should return </p>
  
  <pre><code>id   sender   receiver      time_sent          Message  SendDel RecDel
  4     4          2      2011-08-18 15:19:28     [text]     0      0
  </code></pre>
  
  <p>Im not sure why but my query does not group all the messages together. Here is my query:</p>
  
  <pre><code>SELECT id, sender, receiver, MAX(time_sent), MAX(message) 
  FROM Messages
  WHERE sender='$userid' OR receiver = '$userid' 
  Group By sender,receiver 
  Order BY time_sent DESC
  </code></pre>
  
  <p>Any Solutions?</p>