MySQL - Selecting rows with a minimum number of occurences

Posted by RC on Stack Overflow See other posts from Stack Overflow or by RC
Published on 2010-04-03T10:22:12Z Indexed on 2010/04/03 10:23 UTC
Read the original article Hit count: 230

Filed under:
|

Hi all,

I have this query:

SELECT DISTINCT brand_name FROM masterdata WHERE in_stock = '1' ORDER BY brand_name

It works well, except that I get far too many results. How do I limit this such that rather than just looking for distinct entries, it will only give me distinct entries that exist a minimum of 3 times (for example)?

Basically, if the column had this data...

brand_name
==========
apple
banana
apple
apple
orange
banana
orange
orange

...my current query would return "apple, banana, orange". How do I get it such that it only returns "apple, orange" (ignoring banana because it has less than three occurrences)?

I'm using PHP to build the query, if it matters.

Thanks!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php