所以我有这个问题:
SELECT
AED.aId, CJ.*
FROM
AED
LEFT JOIN
Cronjob as CJ
ON CJ.aID = AED.aId
WHERE
AED.aStatus = '1'
AND
(
CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND
AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR
(
CJ.cjStatus = '9'
OR
CJ.cjStatus = '2'
)
问题是,Cronjob表是空的,如果它是空的仍然想要从AED给出状态为1的所有Id
我找不到任何用途,所以我希望你们能帮忙!
答案 0 :(得分:4)
您应该将所有CJ标准移动到join的ON子句中。
ON (
CJ.aID = AED.aId
AND (cjStatus in ('2','9') OR cjDatum < CURRENT_DATE - INTERVAL 14 DAY ))
选项二是将它们保留在WHERE中,但是对cjStatus和朋友为NULL的情况做出规定(如果没有找到匹配,它们就会出现)。
OR cjStatus IS NULL
答案 1 :(得分:2)
当没有关联的CronJob时,过滤器CJ.cjStatus = '9'
(例如)返回false,因为CJ.cjStatus
为空。这就是LEFT JOIN的作用,当没有对应时返回null字段。
要在要使用LEFT JOIN的表上添加过滤器,filter子句必须位于join子句中,如下所示:
SELECT AED.aId
, CJ.*
FROM AED
LEFT JOIN Cronjob as CJ
ON CJ.aID = AED.aId
AND (CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR (CJ.cjStatus = '9' OR CJ.cjStatus = '2')
WHERE AED.aStatus = '1'
答案 2 :(得分:0)
将OR (CJ.aid is null)
添加到您的AND部分条件中:
SELECT
AED.aId, CJ.*
FROM
AED
LEFT JOIN
Cronjob as CJ
ON CJ.aID = AED.aId
WHERE
AED.aStatus = '1'
AND
(
(
CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND
AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR (CJ.aid is null)
)
OR
(
CJ.cjStatus = '9'
OR
CJ.cjStatus = '2'
)