SELECT *
FROM romantic_dates
WHERE
asker = 'lover' AND date_start BETWEEN NOW() + INTERVAL 90 DAY AND NOW() - INTERVAL 31 DAY
OR
giver = 'lover' AND date_start BETWEEN NOW() + INTERVAL 90 DAY AND NOW() - INTERVAL 31 DAY
ORDER BY date_start DESC;
这是我正在构建的where子句。我很困惑如何使用'或'在now()和interval之间。有人可以提供帮助吗?这个问题与我搜索的其他问题略有不同,因为stackoverflow上有一个或另一个有文档,但没有处理这两个。
答案 0 :(得分:0)
between
的操作数是有序的,所以第一个需要小于第二个。你的表达不是这种情况。所以:
SELECT rd.*
FROM romantic_dates rd
WHERE (asker = 'lover' AND date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY) OR
(giver = 'lover' AND date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY)
ORDER BY date_start DESC;
我倾向于将日期表达式分解出来并使用:
SELECT rd.*
FROM romantic_dates rd
WHERE date_start BETWEEN NOW() - INTERVAL 31 DAY AND NOW() + INTERVAL 90 DAY AND
'lover' in (asker, giver)
ORDER BY date_start DESC;