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!