0
SELECT `groups`.`name`, `groups`.`id`
FROM `groups`
JOIN `group_members`
    ON (`group_members`.`user_id` = `groups`.`user_id`)
WHERE `groups`.`user_id` = '33'
ORDER BY `groups`.`id`

I need to get group's ID and group's name for every group he is member in. But results can't duplicate!

My query returns a lot of duplicate entries.

In groups are stored all groups that do exists (name, description etc.). In group_members are stored all users that are members of some group (user_id, group_id etc.).

How to modify this query to get not-duplicate, but correct results? Thanks in advice!

Edit:

In groups there are no user_id. All member of the group are stored in group_members table. I need to get all groups (group's ID and name) where user is member.

7
  • Why does groups have a user_id? Is it the owner? Commented Jul 22, 2011 at 9:21
  • @cularis : it is not GROUP table representing the groups, it is GROUP_MEMBERS table representing connection between mamber and group... Commented Jul 22, 2011 at 9:22
  • @Daniel thats why its illogical to join on groups.user_id? Commented Jul 22, 2011 at 9:24
  • The way I see it groups contains all groups and group_members has the members of all groups. If you want all groups a user is a member of, you have to join groups with group_members on id/group_id and then use where on group_members.user_id. Commented Jul 22, 2011 at 9:27
  • 1
    You said your query was returning duplicate rows, but then you said groups has no user_id on it. If that's the case, your query doesn't return duplicate rows, it fails, because it has WHERE groups.user_id = 33. Please post the actual table structure and the actual query you're having trouble with, otherwise you're just wasting everyone's time. Commented Jul 22, 2011 at 9:30

4 Answers 4

5

If you want to know all groups of one user, you have to join on the group_members.group_id and edit the WHERE clause.

SELECT `groups`.`name`, `groups`.`id`
FROM `groups`
JOIN `group_members`
    ON (`group_members`.`group_id` = `groups`.`id`)
WHERE `group_members`.`user_id` = '33'
ORDER BY `groups`.`id`
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is the solution. OP has joined on the wrong fields.
this works but it's good to use EXISTS to optimize: stackoverflow.com/questions/6787927/…
1

First, your query states:

`groups`.`user_id` = '33'

This seems incorrect because why would there be a user in the group-table?

Regarding the question. You are probably getting duplicates because a user can exist in more than one group. So, in other words if you only want exactly one group per user then it depends on which group you'd like to see. It could be the minimum group id, maximum or pretty much whatever you'd like.

Comments

0

You have confused some fields i guess, here you go:

SELECT `groups`.`name`, `groups`.`id` 
FROM `groups` 
JOIN `group_members`     
ON `group_members`.`group_id` = `groups`.`id` 
WHERE `groups_members`.`user_id` = '33' 
ORDER BY `groups`.`id

1 Comment

I actually made some mistake because of a wrong assumption, he needs a join from groups to groups_members...
0
SELECT g.name, g.id
FROM groups g
where EXISTS (
  select 'x'
  from group_members gm
  where g.id = gm.group_id
    and gm.user_id = '33'
)
ORDER BY g.id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.