Let’s say I have these table:
What should be my sql request to get the expected result?
table user_group:
group_id | user_id | is_active
1 2 1
1 3 1
2 2 1
4 2 1
table group :
id | name
1 group1
2 group2
3 group3
4 group4
5 group5
6 group5
expected result
group_id | name | user_id | is_active
1 group1 2 1
2 group2 2 1
3 group3 2 0
4 group4 2 1
5 group5 2 0
6 group6 2 0
I tried
SELECT g.id, g.name, ug.user_id, ug.is_active
FROM group g
LEFT OUTER JOIN user_group uc ON ug.group_id = g.id
WHERE ug.user_id =2
but I only have the groups where user 2 is IN (so group1, group2 and group4)
Try this:
You can fiddle with this here