0

我在 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

4 回答 4

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')
于 2013-06-04T13:32:53.953 回答
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'
于 2013-06-04T13:33:26.357 回答
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')
于 2013-06-04T13:33:56.027 回答
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'
于 2013-06-04T13:34:43.703 回答