Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of COUNT to display distinct records with more than 2 occurrences in MySQL

Tags:

mysql

I have a table where I am trying to build a distinct list of all the cities with more than two occurrences in the table. I am trying the current query am am told "function count does not exist"? What am I doing wrong?

SELECT COUNT (city) 
FROM `table1` 
GROUP BY city 
HAVING COUNT (city) >=2
like image 250
Rocco The Taco Avatar asked Oct 24 '25 18:10

Rocco The Taco


1 Answers

Your query is correct you have given a space between COUNT and (City) it must be COUNT(City). that will work fine. Your query should be like this:

SELECT City, COUNT(city) Counts
  FROM `table1` 
 GROUP BY City
HAVING COUNT(city) >=2;

See this SQLFiddle

like image 57
waseemwk Avatar answered Oct 27 '25 07:10

waseemwk