Hello everyone. Thanks in advance for any help.

I have two tables, a groups table and a members table.

The groups table contains the main groups, such as group1, group2, group3.

id | name | description

The members table contains user''s ids and the group id.

id | userid | groupid

I am having difficulty creating a select query that returns all the groups for which a user is a member, including the total amount of members in each group.

Basically, I am trying to achieve the following given a specific user:

GROUP | MEMBERS

Group1 | 12
Group2 | 15

Any ideas how to do this in a single select statement. Thanks very much

Try:

select g.name, count(1)
from group g
where g.id in (select u.groupid from user u where u.userid = <put_userid_here>)
group by g.name

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.