我有以下查询,用于根据 software_id 和级别过滤行。我已将条件放在 ON 条款中,因为我仍然希望返回行,而 JobadvertsSoftware 表中没有相应的行。
SELECT `Jobadvert`.`id` FROM `jobadverts` AS `Jobadvert`
LEFT JOIN `users` AS `User` ON (`Jobadvert`.`user_id` = `User`.`id`)
LEFT JOIN `jobadverts_softwares` AS `JobadvertsSoftware_0` ON
(`Jobadvert`.`id` = 'JobadvertsSoftware_0.jobadvert_id' AND
(`JobadvertsSoftware_0`.`software_id` = '32' AND
`JobadvertsSoftware_0`.`level` IN ('1', 4)))
WHERE `Jobadvert`.`active` = 1 AND `User`.`premium` = '1' AND
Jobadvert`.`department_id` = (5)
GROUP BY `Jobadvert`.`id`
问题是它还返回 JobadvertsSoftware-rows 其中 level 是 2 同样,如果我把它放在 WHERE 子句中,它将过滤掉不应该做的没有 JobadvertsSoftware 的行。如何告诉 MySQL 返回 Jobadvert 的所有行,其中给定的 software_id 和级别匹配或为 NULL?