简体   繁体   中英

sql query for fetching distinct records from table having count greater than 3

i have a table with coloumn id , name and city

I want to fetch those rows where city is same and the number of rows are greater than 3. Is it possible in single sql query?

Yes, it is pretty much the level of query I use to filter out the "I know SQL" people that dont have a clue about the language.

Lets see whether I get that together.

SELECT city, count( ) from TABLE GROUP BY city HAVING COUNT( ) >3

Simple beginner SQL

Not sure mysql supports it ;) But it is apart of the standard for ages.

http://www.w3schools.com/SQL/sql_having.asp

has more explanations.

Yes it is:

SELECT city, COUNT(id) AS [rowcount]
FROM YourTable
GROUP BY city
HAVING COUNT(id) > 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM