I have two tables:
g1 :
id | name |
1 | G1 |
2 | G2 |
3 | G3 |
4 | G4 |
5 | G5 |
g_u_groups :
id | group_id | action |
1 | 2 | view |
2 | 3 | add |
3 | 2 | view |
4 | 3 | view |
5 | 1 | add |
I have a problem with the following query:
SELECT *
FROM g1
LEFT JOIN g_u_groups ON
g_u_groups.group_id = g1.id
AND (g_u_groups.action = 'add' OR g_u_groups.id is null)
ORDER BY g_u_groups.id DESC
LIMIT 10
Now it selects only the groups which have rows in g_u_groups
.
I want to order all groups by latest 'add'
action but I also want to select the groups
which don't have actions
.
The expected results :
id | name |
1 | G1 |
3 | G3 |
2 | G2 |
4 | G4 |
5 | G5 |