title: TYPO3 QueryBuilder - how to find the most recent record for a user?
body: <p>This is a really obvious data problem, but I can't find a simple solution anywhere. </p>
  
  <p>Using TYPO3 QueryBuilder, how do you select the most recent entry for each user from a table that has multiple entries per user?</p>
  
  <pre><code>uid  user_id  value  crdate
  1    1        0      123456
  2    1        1      123400
  3    2        1      123356
  4    2        0      123300
  </code></pre>
  
  <p>I have tried loads of raw SQL approaches and eventually found a method that works, based on this solution - 
  <a href="https://stackoverflow.com/questions/612231/how-can-i-select-rows-with-maxcolumn-value-distinct-by-another-column-in-sql">How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?</a></p>
  
  <pre><code>SELECT * 
  FROM `tx_tablename` AS `tt` 
  INNER JOIN (
      SELECT `uid`, `user_id`, MAX(`crdate`) AS `MaxDateTime` 
      FROM `tx_tablename` 
      GROUP BY `user_id`
  ) AS `groupedtt` 
  ON `tt`.`user_id` = `groupedtt`.`user_id` 
  AND `tt`.`crdate` = `groupedtt`.`MaxDateTime` 
  WHERE `tt`.`consent_content` = 3
  </code></pre>
  
  <p>But I can't see how to reproduce this in QueryBuilder, as the ->join() statement will only accept table names as parameters, not SQL, and ->join() will only accept one joining condition, not two. </p>
  
  <p>Has anyone else found a solution that works in QueryBuilder? 
  Many thanks </p>