0

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

I have a db with thousands of domains and data. I want to list only 100 rows of each domain. i can't use LIMIT 100 cause that only limits 100 records but I want dom1 to list 100 dom2 to list 100 dom3 to list 100

all in one query. This is what I have so far.

    SELECT domain COUNT(Key) AS DomCount
    FROM table_domain
    GROUP BY user,location
    ORDER BY domain,DomCount DESC

Could I use a CASE?

Community
  • 1
  • 1
Dev-Ria
  • 329
  • 1
  • 7
  • 23
  • he talks about getting MIN or MAX of each group. I want to get just a certain number of rows per group. – Dev-Ria Dec 14 '12 at 17:04

1 Answers1

0

Try this

See DEMO for following query here

SELECT key,domain FROM (
SELECT key,domain,
   @drank:=CASE 
               WHEN @domain_v <> domain THEN 1 
               ELSE @drank+1 END AS rn,
   @domain_v:= domain AS domain_set
FROM
  (SELECT @drank:= -1) nr,
  (SELECT @domain_v:= -1) n,
  (SELECT * FROM TableName ORDER BY domain) t
  ) x WHERE rn < 101
rs.
  • 26,707
  • 12
  • 68
  • 90