title: MySQL Selecting the latest payments from a table with a list of users with multiple payments
body: <p>I have a table full of user payments. Each user can have multiple payments. I'm looking to select the latest payment for each user. </p>
  
  <pre><code>SELECT DATE_FORMAT(FROM_UNIXTIME(MAX(p.date)), "%D %M %Y") as last_payment, p.user_id, p.amount, p.period, p.txn_id, u.name 
  FROM payments as p
  INNER JOIN users as u
  ON u.id = p.user_id
  GROUP BY p.user_id
  ORDER BY p.date DESC
  </code></pre>
  
  <p>Now this seems to work fine, the <code>last_payment</code> field is indeed the date of the users latest payment, the only problem is that the other payment fields like <code>p.txn_id</code> and <code>p.amount</code> do not necessarily correspond to the same row as the users last payment, but from a previous payment. </p>
  
  <p>How do I make sure that when i'm choosing the <code>MAX(p.date)</code> it's also choosing the other fields from that same row.</p>
  
  <p>Appreciate the help.</p>