I want to return rows from a MySQL table only if the count of the number of rows matching an index meets a certain number.
Here’s the issue: I have a table of user reviews that are indexed by username. Therefore, I have many rows with different reviews all indexed by usernames. I am creating a search filter, so I need to return the usernames of people who have a certain number of reviews (ie. number of review rows in the table).
How would I create a MySQL query that would pull only usernames of people say that have 4 reviews (4 table row entries)?
This sounds to be a simple
GROUP BYandCOUNT()aggregate using aHAVINGclause to limit to those with 4 reviews.Review the aggregate functions reference for information on how
GROUP BYis used. TheHAVINGclause is applied after theGROUP BYand is used to limit its results.http://sqlfiddle.com/#!2/9dc86/1