title: Doctrine Query with subquery, order by, rand and group by
body: <p>We have a table, from which we want to choose a random row, out of the last 1000 entries. However a user can enter multiple times, and independent of the amount of entries each user should have the same chance. This is currently solved by a native query (see below), which works. I was wondering if it's possible to translate this behaviour to a Doctrine statement, as we're planing to switch from MySQL to PostgreSQL in the future.</p>
  
  <p>Current solution:</p>
  
  <pre><code>$conn = $this-&gt;em-&gt;getConnection();
  $sql = 'SELECT tmp.username FROM (SELECT username, id FROM entries ORDER BY id DESC limit 1000) AS tmp GROUP BY tmp.id ORDER BY RAND() LIMIT 1';
  $stmt = $conn-&gt;prepare($sql);
  $stmt-&gt;execute();
  </code></pre>