Welcome to WebmasterWorld Guest from

Forum Moderators: phranque

Message Too Old, No Replies

MYSQL - Struggling with query

3:19 pm on Aug 12, 2005 (gmt 0)

10+ Year Member

Hereís my problem , hopefully simplified. Have two tables.
Table one contains a list of music genres(only my table has 3,000 types)
Table two contains a lift of albums.
I only want to select a genre that has 5 or more albums.

My attempt so far has been to break the problem into simpler parts, iterating through the Genre table, then performing another query to see if the number of albums for each Genre is greater than 5. As you can imagine this is painfully slow. Iíve had few attempts but only succeeded in locking up my machine :).

If someone could help with problem it would be appreciated.
Many thanks

3:36 pm on Aug 12, 2005 (gmt 0)

10+ Year Member


I depends on how your tables are setup. Does the album table store the genre? If so, then you could do something like this:

SELECT genre, count(*) as cnt
FROM albums
GROUP BY genre
HAVING count(*) >= 5;


4:36 pm on Aug 12, 2005 (gmt 0)

10+ Year Member

Thanks Chad.

Thatís worked great. Thought there might a nice solution, iíve never used GROUP BY and HAVING before :)


6:05 pm on Aug 12, 2005 (gmt 0)

10+ Year Member

You're welcome.

For anyone else reading this thread, HAVING essentially lets you do a WHERE clause after the GROUP BY has aggregated your data. So you can limit queries by min(), max(), count(), etc., which WHERE cannot do.

Glad to hear it worked, humpo. :)



Featured Threads

Hot Threads This Week

Hot Threads This Month