MySQL查询 - 日期/之间的问题

时间:2014-04-18 21:22:59

标签: mysql sql

我有一个显示事件的第三方插件,但是出于某种原因,当有多天的事件时,它会在当天超过开始日期时停止显示事件,即使结束日期仍然在未来。

MySQL查询似乎试图在最后的OR之后用BETWEEN部分返回这些,但它永远不会。我对MySQL不太熟悉,看看我猜错了什么。

例如,我期望它返回的行包含:

published=1 
dates=2014-04-17
enddates=2014-04-19

SQL:

SELECT a.* 
FROM eventlist_events AS a 
WHERE a.published = 1 
AND a.dates >= '2014-04-18' 
AND (DATE(a.dates) = DATE_ADD('2014-04-18',INTERVAL 0 DAY)        
     OR ( a.enddates IS NOT NULL 
          AND (DATE_ADD('2014-04-18',INTERVAL 0 DAY) 
                BETWEEN DATE(a.dates) AND DATE(a.enddates))) ) 

1 个答案:

答案 0 :(得分:0)

要满足2014-04-18落在列值之间的where子句:

SELECT a.* FROM eventlist_events AS a 
WHERE a.published = 1 
  AND a.enddates IS NOT NULL 
  AND '2014-04-18' BETWEEN DATE(a.dates) AND DATE(a.enddates)

如果你想在a.enddates为空时获取,那么

SELECT a.* FROM eventlist_events AS a 
WHERE a.published = 1 
  -- AND a.enddates IS NOT NULL 
  AND '2014-04-18' 
         BETWEEN DATE( a.dates ) 
             AND DATE( IFNULL( a.enddates, a.dates ) )