mysql在左侧填充时连接多个表,并检查某一行是否不为空

7eumitmz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(242)

我希望使用以下结构连接多个表:

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?

问题是,如果解锁图标中没有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

我试图更改join语句的顺序,但没有效果。有办法做到这一点吗?
我正在使用MySQL5.7

uxh89sit

uxh89sit1#

诀窍是在on语句之后包含和:

FROM UnlockedEmoticons ue RIGHT JOIN emoticons e ON ue.emoticonId = e.emoticonId AND ue.userId = #{userId}

并删除where语句。:)

相关问题