1

I am trying to select all of the users who belong to a group. My group_members table keeps track of which groups a user belongs to.

group_members ( group_id , user_id )

So in my group_members table, a user_id may appear multiple times because a user can belong to many groups. I'm using an innerJoin() to pull the user information for each user_id. However, I only want to display a user's name once when I retrieve it.

How can I do this?

1 Answer 1

3

Use DISTINCT. Either:

select distinct u.* from users u
  join group_members gm on gm.user_id = u.user_id
 where ...

or

select * from users u
 where u.user_id in (select user_id from group_members where ...)
   ...

Run "EXPLAIN SELECT" on both queries to see which one is faster in your case.

Sign up to request clarification or add additional context in comments.

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.