I have 4 tables in my database:
users (id,name)
roles (id,name)
positions (id,name)
position_user (user_id,position_id)
- Relationship between users to roles is one to one
- Relationship between users positions is many to many
i want to take all users with their role name and list with their positions but i don't know how to structure my query. I think that one of my query must be something like this:
SELECT pu.user_id AS user_id,
group_concat(p.name separator ',') AS list_pos
FROM position_user pu
INNER JOIN positions p
ON p.id = pu.position_id
GROUP BY pu.user_id
And other one must be like this :
SELECT users.id, users.first_name, roles.name
FROM users
JOIN roles
ON users.role_id = roles.id
Can I combine these two in one query and how ?