Answer the question
In order to leave comments, you need to log in
Need to improve sql query.?
I have these tables:
Groups:
id
name
and
Group Members:
id
groupId
memberId
We have a user id.
You need to get a list of groups to which he is subscribed with the number of group subscribers and the name of the group.
I have the following sql query to get a list of groups, but I can't get the number of group subscribers:
SELECT g.gid, g.name
FROM groups g, groupmembers gm
WHERE gm.memberId = 1 AND g.gid = gm.groupId
Answer the question
In order to leave comments, you need to log in
SELECT g.gid, g.name, (SELECT COUNT(*) FROM groupmembers m WHERE m.gid = g.groupId) AS CoungGM
FROM groups g, groupmembers gm
WHERE gm.memberId = 1 AND g.gid = gm.groupId
Get the group list like this
select
g.gid,
g.name
from groups g
inner join groupmembers gm ON gm.memberId = 1 AND g.gid = gm.groupId
select
g.name,
COUNT(gm.id) as CountGM
from groups g
inner join groupmembers gm ON gm.memberId = 1 AND g.gid = gm.groupId
group by g.name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question