title: How to only get DISTINCT rows from a JOIN query: MySQL
body: <p>Two tables. Table one is users. Table two is user events. One user has many events. I just want to get a list of the users with the most recent events. I can obviously set it to select more rows than I need and filter out the duplicates but I really want to just select the exact number of rows I need in the SQL.</p>
  
  <p>This SQL query gives duplicates of <code>a.name</code> where one user has had more than one event recently. I thought that <code>DISTINCT</code> would prevent duplicates. What am I doing wrong? How do I just get one row for each user?</p>
  
  <pre><code>SELECT DISTINCT (a.name), b.date , a.id
  FROM `users` a
  INNER JOIN events b
  ON a.id = b.userid 
  WHERE now() &gt; b.date 
  ORDER BY b.date DESC, a.id DESC
  LIMIT 30;
  </code></pre>
  
  <p>Table users...</p>
  
  <pre><code>id name
  </code></pre>
  
  <p>Table events...</p>
  
  <pre><code>id userid date
  </code></pre>
  
  <p>I don't need to select the <code>b.date</code> value but this is needed for <code>DISTINCT</code> to work because it is in the <code>ORDER BY</code> part, I believe.</p>