0

我有以下查询,用于根据 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?

4

4 回答 4

0

尝试这个:

SELECT `Jobadvert`.`id`, `JobadvertsSoftware_0`.`level` 
FROM `jobadverts` AS `Jobadvert` 

LEFT JOIN `users` AS `User` ON (`Jobadvert`.`user_id` = `User`.`id`) 

INNER 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`

萨鲁多斯!

于 2013-03-14T14:51:22.420 回答
0

假设连接不需要 ON 子句中的级别参数,您可以在 Software 表上执行嵌套 SELECT 以首先清除不需要的数据:

SELECT * FROM jobadverts_softwares
WHERE 
   (`software_id` = 32 OR `software_id` IS NULL) --Select all software_id that are 32 or null
AND
   `level` IN (1, 4)

然后,您可以将其作为嵌套语句合并到您的主 SQL 查询中,这样您只需加入在 LEFT JOIN 中过滤的数据,但保留您需要的任何空值:

SELECT `Jobadvert`.`id` 
FROM `jobadverts` AS `Jobadvert` 

LEFT JOIN `users` AS `User`
ON `Jobadvert`.`user_id` = `User`.`id`

LEFT JOIN 
(   --Software Subquery
    SELECT `jobadvert_id`, `level` FROM jobadverts_softwares
    WHERE 
       (`software_id` = 32 OR `software_id` IS NULL) --Select all software_id that are 32 or null
    AND
       `level` IN (1, 4)
) AS `software_subquery`

ON `Jobadvert`.`id` = `software_subquery`.`jobadvert_id`

WHERE
    `Jobadvert`.`active` = 1
AND 
    `User`.`premium` = '1'
AND 
    `Jobadvert`.`department_id` = 5 

ORDER BY `Jobadvert`.`id` --Changed GROUP BY to ORDER BY as not needed

这是未经测试的,但请尝试一下,看看这是否会有所帮助。

于 2013-03-14T15:02:30.757 回答
0

试试这个(如果某些字段在字符串上是数字有点不清楚,可能会更正):

SELECT distinct(`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`
WHERE 
`Jobadvert`.`active` = 1 
AND `User`.`premium` = '1' 
AND Jobadvert`.`department_id` = (5) 
AND JobadvertsSoftware_0`.`software_id` = '32' 
AND (`JobadvertsSoftware_0`.`level` IN (1, 4) OR `JobadvertsSoftware_0`.`level` is NULL)
于 2013-03-14T14:53:26.637 回答
-1

尝试这个:

SELECT j.id 
FROM jobadverts j 
LEFT JOIN User u ON (j.user_id = u.id) 
LEFT JOIN jobadverts_softwares AS js ON
(j.id = js.jobadvert_id)
WHERE j.active = 1 
  AND u.premium = '1' 
  AND j.department_id = (5) 
  AND js.software_id` = '32' 
  AND js.level IN ('1', 4))) 

除非以某种方式对数据求和,否则您不需要 GROUP BY。

于 2013-03-14T15:00:25.153 回答