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
Release: 2025-01-01 03:24:09
Original
788 people have browsed it

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
Copy after login

Answer:

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template