title: SQL getting max id field on a LEFT JOIN
body: <p>I am trying to pull the photo from tblimage that corresponds to the maxid in the tblimage for each user.  Currently, I am getting all the messages from the message table and a random photo for the user that posted the message, I would like the photo to be the latest uploaded photo. the way its written now it just pulls a random photo from the table.  any suggestions? </p>
  
  <p>table structures are as such: </p>
  
  <blockquote>
    <p>messages:  msgid, message, user_id, event_id<br>
     tblimage: id, photo, userid</p>
  </blockquote>
  
  <pre><code>SELECT messages.*, tblimage.photo, max(tblimage.id) 
          FROM messages LEFT JOIN tblimage ON messages.user_id = tblimage.userid 
          GROUP BY messages.msg_id, messages.user_id 
          ORDER BY messages.msg_id DESC, tblimage.id desc
  </code></pre>