0

Possible Duplicate:
mysql: Using LIMIT within GROUP BY to get N results per group?

I tried group by, but then I'll only get 1 per account_id, I want 10 per account_id. Here's what I'm looking for.

SELECT id,account_id FROM whatever {LIMIT to 10 of each account_id}
Community
  • 1
  • 1
Farzher
  • 13,934
  • 21
  • 69
  • 100

1 Answers1

0

You can use rank for each account_id, id group

Example:

SELECT id,account_id FROM (
SELECT id,account_id,
   @acrank:=CASE 
               WHEN @aid <> id THEN 1 
               ELSE @acrank+1 END AS rn,
   @id:=id AS id_set
FROM
  (SELECT @acrank:= -1) nr,
  (SELECT @id:= -1) n,
  (SELECT * FROM Test ORDER BY id, account_id) t
  ) x WHERE rn < 11
rs.
  • 26,707
  • 12
  • 68
  • 90