我有两张桌子:
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 |
我遇到以下问题的问题:
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
现在它只选择g_u_groups
中有行的组。
我想按最新的'add'
操作订购所有群组,但我还想选择没有groups
的{{1}}。
预期结果:
actions
答案 0 :(得分:0)
这会返回预期的吗?
SELECT g1.id, g1.name
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, g1.id DESC
LIMIT 10
这个POST谈论排序多个列,它可以帮助你
答案 1 :(得分:0)
这是通过最新添加操作返回的SQL,也显示没有操作的项目:
SELECT *
FROM g1
LEFT JOIN g_u_groups ON
g_u_groups.group_id = g1.id
where (g_u_groups.action = 'add' OR g_u_groups.id is null)
ORDER BY g_u_groups.id DESC
LIMIT 10;