MySQL,检查范围之间的多个日期

时间:2013-06-04 13:28:34

标签: mysql

我在db行中有三个日期列。这些列彼此独立,因为它们中的任何一个都可以填充或为NULL。

我想检查以查找任何日期列属于已知范围的所有行。

这是我在WHERE子句中的内容:

WHERE (d.date1 OR d.date2 OR d.date3) 
    BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'

任何有关为什么不起作用的想法都将不胜感激。

非常感谢

4 个答案:

答案 0 :(得分:1)

你误解了逻辑“或”的运作方式。

WHERE (d.date1 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46') 
   OR (d.date2 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46') 
   OR (d.date3 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46')

答案 1 :(得分:0)

您必须分别指定这三个部分。

请注意,如果任何日期都有值,(d.date1 OR d.date2 OR d.date3)将评估为true,因此您的WHERE子句目前与此相当:

WHERE TRUE BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'

所以你需要分别测试每个日期:

WHERE d.date1 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'
   OR d.date2 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'
   OR d.date3 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'

答案 2 :(得分:0)

你应该这样做:

WHERE (d.date1 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46')
OR (d.date2 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46')
OR (d.date3 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46')

答案 3 :(得分:0)

因为Between运算符需要Between之间的单个值。颠倒序列......像这样:

WHERE d.date1 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46' Or
      d.date2 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46' Or
      d.date3 BETWEEN '2011-11-09 13:08:46' AND '2011-11-11 16:08:46'