我希望使用以下结构连接多个表:
Users (userId, name)
Emoticons (emoticonId, name)
UnlockedEmoticons (userId, emoticonId, name)
对于每个项目,我希望显示某个表情符号是否被用户解锁,如:
1(emoticonId), emoticonA(name), unlocked (userId IS NOT NULL) AS has?,
2(emoticonId), emoticonB(name), locked (userId IS NOT NULL) AS has?
问题是,如果unlockedEmoticons中没有userId,我会得到空结果。
SELECT e.*, (ue.id IS NOT NULL) AS `has`
FROM UnlockedEmoticons ue RIGHT JOIN emoticons e ON ue.emoticonId = e.emoticonId
LEFT JOIN users u ON u.id = ue.userId
WHERE uk.userId = 33
我尝试将连接语句订单更改为但无效。有没有办法实现这个目标?
我正在使用mysql 5.7
答案 0 :(得分:0)
诀窍是在ON语句后包含AND:
FROM UnlockedEmoticons ue RIGHT JOIN emoticons e ON ue.emoticonId = e.emoticonId AND ue.userId = #{userId}
并删除WHERE语句。 :)