2

I want to select 3 record from each group by mysql query my table structure is like this -

id | customer | catId
---------------------
 1 | Joe      | 2
 2 | Sally    | 2
 3 | Joe      | 2
 4 | Sally    | 2
 5 | Joe      | 2
 6 | Sally    | 3
 7 | Joe      | 3
 8 | Sally    | 3
 9 | Joe      | 3
10 | Sally    | 4
11 | Joe      | 4
12 | Sally    | 4

I want to select 3 records for each distinct catId means

id | customer | catId
---------------------
 1 | Joe      | 2
 2 | Sally    | 2
 3 | Joe      | 2
 6 | Sally    | 3
 7 | Joe      | 3
 8 | Sally    | 3
10 | Sally    | 4
11 | Joe      | 4
12 | Sally    | 4

I tried this query but its showing only one record for each distinct catId. l

SELECT * FROM table GROUP BY CatIds

and I am getting

id | customer | catId
---------------------
 1 | Joe      | 2
 6 | Sally    | 3
10 | Sally    | 4
Khoyendra Pande
  • 1,627
  • 5
  • 25
  • 42
  • 2
    Have a look at this answer, it is basically the same thing : http://stackoverflow.com/questions/3140589/mysql-query-select-top-3-rows-from-table-for-each-category – adrien May 07 '12 at 10:55

1 Answers1

-2

You may just run 3 queries instead.

SELECT * FROM table WHERE catId=2 LIMIT 3
SELECT * FROM table WHERE catId=3 LIMIT 3
SELECT * FROM table WHERE catId=4 LIMIT 3
Konstantin Pereiaslov
  • 1,786
  • 1
  • 18
  • 26