64

I have two tables: groups and group_members.

The groups table contains all the information for each group, such as its ID, title, description, etc.

In the group_members table, it lists all the members who are apart of each group like this:

group_id | user_id
1 | 100
2 | 23
2 | 100
9 | 601

Basically, I want to list THREE groups on a page, and I only want to list groups which have MORE than four members. Inside the <?php while ?> loop, I then want to four members who are apart of that group. I'm having no trouble listing the groups, and listing the members in another internal loop, I just cannot refine the groups so that ONLY those with more than 4 members show.

Does anybody know how to do this? I'm sure it's with MySQL joins.

hohner
  • 11,498
  • 8
  • 49
  • 84

4 Answers4

117

MySQL use HAVING statement for this tasks.

Your query would look like this:

SELECT g.group_id, COUNT(m.member_id) AS members
FROM groups AS g
LEFT JOIN group_members AS m USING(group_id)
GROUP BY g.group_id
HAVING members > 4

example when references have different names

SELECT g.id, COUNT(m.member_id) AS members
FROM groups AS g
LEFT JOIN group_members AS m ON g.id = m.group_id
GROUP BY g.id
HAVING members > 4

Also, make sure that you set indexes inside your database schema for keys you are using in JOINS as it can affect your site performance.

bobsoap
  • 4,844
  • 7
  • 30
  • 43
Nazariy
  • 6,028
  • 5
  • 37
  • 61
  • I altered the SQL to fit my code: `SELECT g.id, COUNT(m.id_profile) AS members FROM groups_main AS g LEFT JOIN groups_fans AS m USING(id) GROUP BY g.id HAVING members > 4` Results in this MySQL error: "Unknown column 'id' in 'from clause'. – hohner Jan 31 '11 at 02:36
  • You can use USING statement in only if two columns carry same name, in other cases use ON statement I would update my answer. – Nazariy Jan 31 '11 at 02:39
63
SELECT DISTINCT groups.id, 
       (SELECT COUNT(*) FROM group_members
        WHERE member_id = groups.id) AS memberCount
FROM groups
Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
Reena Shirale
  • 1,992
  • 1
  • 17
  • 15
2

Maybe I am off the mark here and not understanding the OP but why are you joining tables?

If you have a table with members and this table has a column named "group_id", you can just run a query on the members table to get a count of the members grouped by the group_id.

SELECT group_id, COUNT(*) as membercount 
FROM members 
GROUP BY group_id 
HAVING membercount > 4

This should have the least overhead simply because you are avoiding a join but should still give you what you wanted.

If you want the group details and description etc, then add a join from the members table back to the groups table to retrieve the name would give you the quickest result.

crazeyez
  • 459
  • 4
  • 4
1

Your groups_main table has a key column named id. I believe you can only use the USING syntax for the join if the groups_fans table has a key column with the same name, which it probably does not. So instead, try this:

LEFT JOIN groups_fans AS m ON m.group_id = g.id

Or replace group_id with whatever the appropriate column name is in the groups_fans table.

Kristian
  • 21,204
  • 19
  • 101
  • 176
bbb
  • 11
  • 1