我有这个查询
SELECT `badges`.`id`,
`badges`.`en_seo`
FROM `badges`
WHERE `status` IS NULL
AND `id` NOT IN
(SELECT `badges_id`
FROM `users_badges`
WHERE `users_id` = 1)
我可以使用JOINS重写此代码吗?
答案 0 :(得分:1)
与查询完全等效的是:
SELECT `badges`.`id`,
`badges`.`en_seo`
FROM `badges`
LEFT JOIN `users_badges`
ON `badges`.`id` = `users_badges`.`badges_id`
AND `users_badges`.`users_id` = 1
WHERE `badges`.`status` IS NULL
AND `users_badges`.`users_id` IS NULL
然而,在Juan的回答中使用别名是一种常见的做法。
答案 1 :(得分:0)
始终尝试为表名使用别名
SELECT b.`id`,
b.`en_seo`
FROM `badges` b
LEFT JOIN `users_badges` ub
ON b.`id`= ub.`badges_id`
and ub.`users_id` = 1
WHERE b.`status` IS NULL
AND ub.`id` IS NULL