Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you limit the group by rows in an mysql query?

Tags:

mysql

There are other questions on here that sound similar but are not. I have a query that returns a bunch of rows with group by and I want to apply a limit to the total group by rows, not the total rows used to create the groups.

ID    TYPE        COLOR        SIZE
----------------------------------------
1     Circle      Blue         Large
2     Circle      Red          Large
3     Square      Green        Large
4     Circle      Purple       Large
5     Circle      Blue         Small
6     Circle      Yellow       Medium
7     Circle      Black        Large
8     Oval        Blue         Large
9     Circle      Gray         Small
10    Triangle    Black        Large
11    Star        Green        Large
12    Triangle    Purple       Large

SELECT size, type FROM clothes WHERE size = 'large' GROUP BY type LIMIT 0, 5

TYPE       SIZE       ROWS
---------------------------    
Circle     Large      4
Square     Large      1

^^^^ 2 GROUP BY ROWS THAT HAVE ALREADY EXHAUSTED MY LIMIT

TYPE       SIZE       ROWS
---------------------------    
Circle     Large      4
Square     Large      1
Oval       Large      1
Triangle   Large      2
Star       Large      1

^^^^ HERE'S WHAT I WANT, LIMIT APPLIED TO THE GROUPS

There must be some subquery or something I can do here, but I'm not figuring it out.

Thanks.

like image 382
Tomas Avatar asked Jun 15 '12 22:06

Tomas


People also ask

How do I limit rows in MySQL?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

Can I use limit with GROUP BY in SQL?

No, you can't LIMIT subqueries arbitrarily (you can do it to a limited extent in newer MySQLs, but not for 5 results per group). This is a groupwise-maximum type query, which is not trivial to do in SQL.

Can I use limit with GROUP BY?

Using GROUP BY with LIMITIf you don't group by any columns, you'll get a 1-row result—no problem there. If you group by a column with enough unique values that it exceeds the LIMIT number, the aggregates will be calculated, and then some rows will simply be omitted from the results.

How do I limit the number of rows returned?

You use the LIMIT clause to constrain the number of rows returned by the query. For example, a SELECT statement may return one million rows. However, if you just need the first 10 rows in the result set, you can add the LIMIT clause to the SELECT statement to retrieve 10 rows.


2 Answers

This works for me:

SELECT type, size, COUNT(*) AS rows
FROM clothes
WHERE size = 'large'
GROUP BY type
LIMIT 0, 5

Results in:

type      size   rows
------------------------
Circle    Large     4
Oval      Large     1
Square    Large     1
Star      Large     1
Triangle  Large     2

LIMIT should get applied after GROUP BY, so I don't understand the issue.

like image 155
Ami Avatar answered Oct 05 '22 22:10

Ami


SELECT * FROM (
  SELECT id, color, size, type FROM clothes WHERE size = 'large' GROUP BY type 
) AS baseview LIMIT 0, 25
like image 33
Eugen Rieck Avatar answered Oct 06 '22 00:10

Eugen Rieck