Reputation: 263
I have this query
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)
Can I rewrite this code using JOINS?
Upvotes: 0
Views: 138
Reputation: 25351
The exact equivalent to your query is:
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
However, it's a common practice to use aliases like in Juan's answer.
Upvotes: 1
Reputation: 48187
Always try to use alias for your table names
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
Upvotes: 0