Home >Database >Mysql Tutorial >Can I Filter Grouped Results Based on Row Count?

Can I Filter Grouped Results Based on Row Count?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-01 03:24:09785browse

Can I Filter Grouped Results Based on Row Count?

Filtering Based on Grouped Row Count

Question:

Can results be grouped and subsequently filtered based on the number of rows in each group?

Example Query:

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

Answer:

To achieve the desired filtering, the HAVING clause should be employed instead:

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

By using HAVING, you can specify conditions that apply to the grouped results, in this case filtering for groups with more than one row.

The above is the detailed content of Can I Filter Grouped Results Based on Row Count?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn