Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by COUNT(*)?

Tags:

sql

mysql

Is it possible to group results and then filter by how many rows are in the group?

Something like this:

SELECT * FROM mytable WHERE COUNT(*) > 1 GROUP BY name 
like image 569
DonutReply Avatar asked Apr 14 '11 13:04

DonutReply


2 Answers

You want to use HAVING to filter on the aggregate function.

SELECT name, COUNT(*)     FROM mytable     GROUP BY name     HAVING COUNT(*) > 1 
like image 142
Joe Stefanelli Avatar answered Sep 26 '22 14:09

Joe Stefanelli


You need to use HAVING

SELECT * FROM mytable GROUP BY name HAVING COUNT(*) > 1 

Although, SELECT * doesn't make much sense when you're grouping. I assume it's just for an example

like image 45
JohnP Avatar answered Sep 25 '22 14:09

JohnP