title: pick random rows from randomly selected userid's collection [mysql]
body: <p>The scenario: (I'm using MySQL)</p>
  
  <p>Here is my schema:</p>
  
  <pre><code>CREATE TABLE so_time_diff(
    OwnerUserId int(11),
    time_diff int(10)
  );
  </code></pre>
  
  <p>There are many OwnerUserId's with each OwnerUserId having many time_diff values. </p>
  
  <p>I would like to pick 1000 random distinct OwnerUserIds and for each OwnerUserId, pick just one random time_difference value. </p>
  
  <p>I already got 1000 distinct OwnerUserIds from else where and stored in a different table:</p>
  
  <pre><code>mysql&gt; create table so_OwnerUserId select distinct(Id) as OwnerUserId
  from so_users order by RAND() limit 1000;
  </code></pre>
  
  <p>I have written the following query:</p>
  
  <pre><code>select @td := time_diff from so_time_diff sotd, so_OwnerUserId soui 
  where sotd.OwnerUserId = soui.OwnerUserId group by sotd.OwnerUserId
  order by rand() limit 1;
  </code></pre>
  
  <p>This doesn't seem to accomplish what I want. It obviously returns just one row. But I want one random row from each OwnerUserId's time_diff collection. Could someone guide me on how to accomplish this?</p>
  
  <p>FYI - the size of dataset is huge - ~56m records. So I'm looking for an optimal query.</p>
  
  <p>Any help appreciated.</p>
  
  <p>Thanks!</p>