I have the following tables:
Table: user_groups (many-to-many)
- user_id (int)
- group_id (varchar)
Table: profile_groups (many-to-many)
- profile_id (int)
- group_id (varchar)
So basically, I want to write a sql script to find out what profile is assigned to each user.
So in the end there should be only 2 columns: user_id and profile_id.
How would I go about doing this?
Edit: It’s actually a lot more complicated than a simple join.
E.g.
User_groups may have the following rows
- 1 group1
- 1 group2
- 1 group3
- 2 group1
- 2 group2
- 3 group4
and profile_groups may have the following:
- 11 group1
- 11 group2
- 11 group3
- 21 group1
- 21 group2
- 22 group4
So the result should be
- 1 11
- 2 21
- 3 22
Each user should only have ONE profile
I just saw a question like this the other day. I think the hard part here is you’re looking for user_id/profile_id combinations where the user_id has every group_id that the profile_id has, no more and no less. So take the usual join and add some correlation to count the number of group_ids each profile/user has and make sure they match (this has been edited a few times):
Here’s a run which includes two profiles with three groups each, with one common group between them and a new user in the fourth profile: