我在SQL上并不是非常棒。我正在使用以下内容过滤concrete5集合,但不确定如何使其全部工作。如何正确使用括号分隔AND和OR语句?
这没有做任何事情:
$this->filter(null, "(
(DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
OR
(DATE_SUB(expirationDate, INTERVAL 5 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
OR
(DATE_SUB(expirationDate, INTERVAL 1 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
)
AND
(DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())");
这一个主要起作用(在AND之前的语句周围删除了括号)但似乎没有在AND ...“DATE_ADD(warnedDate,INTERVAL 4 DAY)&lt; = NOW())”之后过滤该子句:
$this->filter(null, "
(DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
OR
(DATE_SUB(expirationDate, INTERVAL 5 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
OR
(DATE_SUB(expirationDate, INTERVAL 1 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
AND
(DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())");
答案 0 :(得分:0)
如评论中所述,5天和1天的两个条款是多余的,因为到今天的30天间隔已包括今天的5,1天,因此将其删除。 试试这样的事情
(DATE_SUB(expirationDate, INTERVAL 30 DAY) BETWEEN DATE_SUB(NOW(), INTERVAL 3 DAY) AND NOW())
AND
(DATE_ADD(warnedDate, INTERVAL 4 DAY) <= NOW())
注意:当您希望条件为真时使用AND
,并且至少有一个条件为OR
为真。