title: mysql retrive data giving limit and sub limit
body: <p>I have a table named <code>item</code> with category ID and item IDs. 
  Item ID is the primary key and each item is related to category ID.</p>
  
  <p>I want to retrieve 20 items, but 4 items for one category. If there aren't 4 items for
  some category, then select all items from this category.</p>
  
  <p>This is my query, it is not working:</p>
  
  <pre><code>SELECT * FROM `item`
  JOIN (SELECT `item_id` FROM `item` GROUP BY `category_id` LIMIT 4) LIMIT 20;
  </code></pre>
  
  <p>my table stucture</p>
  
  <p>item</p>
  
  <pre><code>item id     |   category id
  ----------------------------
  1           |   1
  2           |   1
  3           |   2
  4           |   2
  5           |   1
  6           |   3
  </code></pre>
  
  <p>if my table has</p>
  
  <p>category</p>
  
  <pre><code>category    |   count(item)
  -----------------------------
  1           |   5
  2           |   3
  3           |   10
  4           |   8   
  5           |   12
  6           |   6
  7           |   6
  8           |   6
  </code></pre>
  
  <p>I want to select 20 items like that
  category  items from this category</p>
  
  <pre><code>----------
  1   |   4
  2   |   3
  3   |   4
  4   |   4
  5   |   4
  6   |   1
  </code></pre>