title: LIMIT results to n unique column values?
body: <p>I have some MySQL results like this:</p>
  
  <pre><code>---------------------------
  | name | something_random |
  ---------------------------
  | john | ekjalsdjalfjkldd |
  | alex | akjsldfjaekallee |
  | alex | jkjlkjslakjfjflj |
  | alex | kajslejajejjaddd |
  |  bob | ekakdie33kkd93ld |
  |  bob | 33kd993kakakl3ll |
  | paul | 3k309dki595k3lkd |
  | paul | 3k399kkfkg93lk3l |
  etc...
  </code></pre>
  
  <p>This goes on for 1000's of rows of results. I need to limit the number of results to the first 50 unique names. I think there is a simple solution to this but I'm not sure.</p>
  
  <p>I've tried using derived tables and variables but can't quite get there. If I could figure out how to increment a variable once every time a name is different I think I could say <code>WHERE variable &lt;= 50</code>.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I've tried the Inner Join approach(es) suggested below. The problem is this:</p>
  
  <p>The subselect <code>SELECT DISTINCT name FROM testTable LIMIT 50</code> grabs the first 50 distinct names. Perhaps I wasn't clear enough in my original post, but this limits my query too much. In my query, not every name in the table is returned in the result. Let me modify my original example:</p>
  
  <pre><code>----------------------------------
  |   id | name | something_random |
  ----------------------------------
  |    1 | john | ekjalsdjalfjkldd |
  |    4 | alex | akjsldfjaekallee |
  |    4 | alex | jkjlkjslakjfjflj |
  |    4 | alex | kajslejajejjaddd |
  |    6 |  bob | ekakdie33kkd93ld |
  |    6 |  bob | 33kd993kakakl3ll |
  |   12 | paul | 3k309dki595k3lkd |
  |   12 | paul | 3k399kkfkg93lk3l |
  etc...
  </code></pre>
  
  <p>So I added in some id numbers here. These ID numbers pertain to the people's names in the tables. So you can see in the results, not every single person/name in the table is necessarily in the result (due to some WHERE condition). So the 50th distinct name in the list will always have an ID number higher than 49. The 50th person could be id 79, 234, 4954 etc...</p>
  
  <p>So back to the problem. The subselect <code>SELECT DISTINCT name FROM testTable LIMIT 50</code> selects the first 50 names in the table. That means that my search results will be limited to names that have ID &lt;=50, which is too constricting. If there are certain names that don't show up in the query (due to some WHERE condition), then they are still counted as one of the 50 distinct names. So you end up with too few results.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>To @trapper: This is a basic simplification of what my query looks like:</p>
  
  <pre><code>SELECT
      t1.id,
      t1.name,
      t2.details
  FROM t1
  LEFT JOIN t2 ON t1.id = t2.some_id
  INNER JOIN
      (SELECT DISTINCT name FROM t1 ORDER BY id LIMIT 0,50) s ON s.name = t1.name
  WHERE
      SOME CONDITIONS
  ORDER BY
      t1.id,
      t1.name
  </code></pre>
  
  <p>And my results look like this:</p>
  
  <pre><code>----------------------------------
  |   id | name |          details |
  ----------------------------------
  |    1 | john | ekjalsdjalfjkldd |
  |    3 | alex | akjsldfjaekallee |
  |    3 | alex | jkjlkjslakjfjflj |
  |    4 | alex | kajslejajejjaddd |
  |    6 |  bob | ekakdie33kkd93ld |
  |    6 |  bob | 33kd993kakakl3ll |
  |   12 | paul | 3k309dki595k3lkd |
  |   12 | paul | 3k399kkfkg93lk3l |
  ...
  |   37 | bill | kajslejajejjaddd |
  |   37 | bill | ekakdie33kkd93ld |
  |   41 | matt | 33kd993kakakl3ll |
  |   50 | jake | 3k309dki595k3lkd |
  |   50 | jake | 3k399kkfkg93lk3l |
  ----------------------------------
  </code></pre>
  
  <p>The results stop at id=50. There are NOT 50 distinct names in the list. There are only roughly 23 distinct names.</p>