I realize there are several questions similar to this one already out there (here are three), but I've read through many of them and can't align the answers there with my particular situation (i.e., I'm a braindead n00b).
MY ULTIMATE GOAL
To create a list of the "most popular" groups, with the most popular at the top. Popularity is determined by the number of members. Each group has a count of members and quotes; I'd like to run a single query to get both counts.
TABLE STRUCTURE
group_members
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| member_id | int(10) | NO | PRI | None | |
| group_id | int(10) | NO | PRI | None | |
+--------------------+------------------+------+-----+---------+----------------+
group_quotes
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| quote_id | int(10) | NO | PRI | None | |
| group_id | int(10) | NO | PRI | None | |
+--------------------+------------------+------+-----+---------+----------------+
group_data
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| entry_id | int(10) unsigned | NO | PRI | None | auto_increment |
| title | varchar(100) | NO | | None | |
| url_title | varchar(75) | No | | None | |
+--------------------+------------------+------+-----+---------+----------------+
CURRENT (BROKEN) QUERY
I've tried the following (and close variations of it), but the result is a multiple of the number of entries in each table:
SELECT group_quotes.group_id, COUNT(group_quotes.group_id) AS numquotes, COUNT(group_members.group_id) AS nummembers
FROM group_quotes, group_members
GROUP BY group_quotes.group_id
ORDER BY numquotes
DESC LIMIT 5
...within the query loop, output the group's title, etc...
For example: when I last ran this query, there were 7 entries in the group_members
table and 3 instances of group_id #10 in the group_quotes
table; I got 21 for both the member count and quote count for group #10 (the correct numbers should be 2 and 3).
Would anyone mind taking a look and letting me know where I'm going wrong?