dallen
dallen

Reputation: 2651

Getting users based on group, but users, user_groups and groups kept in separate tables

I'm assuming I need to use JOIN, but I'm not totally getting it. Here's my situation. I'm using ion_auth. It stores users in 'users', groups in 'groups' and stores users' group in 'users_groups'.

So if I have a group 'sales' with an id of '2', any user associated with that group appears in 'users_groups' as:

id  user_id  group_id
1   1        2
1   2        2

So how can I quickly return the results of a query like: get users from group 2. Would the JOIN command be what I'm looking for? I'm using CodeIgniter's Active Record but am open to any suggestions.

Upvotes: 0

Views: 80

Answers (2)

Chibuzo
Chibuzo

Reputation: 6117

SELECT list_of_fields FROM users JOIN users_group USING (group_id) WHERE users.group_id = 2

Assuming you used group_id as the field name on both tables

Upvotes: -1

dorsh
dorsh

Reputation: 24730

It's a very basic join between two tables

SELECT users.* FROM users JOIN users_groups ON (users.id=users_groups.user_id) WHERE users_groups.group_id = 2

Upvotes: 2

Related Questions