I have one table of users.
users
| user_id | name |
| ------- | ------ |
| 1 | Jerry |
| 2 | George |
| 3 | Elaine |
| 4 | Kramer |
I have one table that links roles to users, and roles are assigned at a tree.
user_roles
| user_id | role_id | tree_id |
| ------- | ------- | ------- |
| 1 | 5 | 1 |
| 1 | 5 | 2 |
| 2 | 6 | 1 |
| 3 | 7 | 1 |
| 4 | 8 | 1 |
I need to only return results where a user's role is assigned at a certain tree_id, so I'm checking all the roles and trees. At the end I want it to return one row per user.
I'm using Knex and doing a query that looks like:
knex('users')
.leftJoin('user_roles', {'user.user_id': 'user_roles.user_id'})
.whereIn('user_roles.tree_id', arrayOfTreeIds)
.andWhere(moreFilters)
SELECT *
FROM users
LEFT JOIN user_roles on users.user_id = user_roles.user_id
WHERE user_roles.tree_id in (1, 2, 3)
I'm getting five results back instead of four, though. If I try to SELECT DISTINCT it tells me I need to GROUP BY, but I can't get that to work. What do I need to do to get only one result per user id?