mysql选择30天范围内的日期 [英] mysql select dates in 30-day range

查看:540
本文介绍了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

这篇关于mysql选择30天范围内的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆