mysql选择30天范围内的日期

问题描述:

这必须很简单,但是我弄弄了它,却没有得到我想要的任何东西.我有以下代码:

This must be simple but I fiddled with it, and didn't get anything I wanted. I have the following code:

SELECT id,title,start_date 
  FROM events 
 WHERE start_date > DATE_SUB(NOW(), INTERVAL 1 MONTH) 
  AND city = '$cityName' 
ORDER BY start_date DESC

现在,这将选择具有该月日期的事件,但是查询中显示的本月的定义与我所需要的不同.我需要它向我展示30天内的事件,而不仅仅是本月(即8月).如果我在八月插入一个事件,它将显示结果.如果我确实插入9月,即使距离日期不到30天也不会.

Now this selects events with dates in this month, but the definition of this month shown in query is different than what I need. I need it to show me events within 30 days and not only this month i.e. august. If I insert an event in august it shows the outcome. If I do insert september, it doesn't even though it is less than 30 days away.

您应将1 MONTH更改为30 DAY:

WHERE start_date > NOW() - INTERVAL 30 DAY

要将其在任一方向上限制为30天:

To limit it to 30 days in either direction:

WHERE start_date > NOW() - INTERVAL 30 DAY
AND start_date < NOW() + INTERVAL 30 DAY