mysql时间条件30天内_mysql选择30天范围内的日期

bd96500e110b49cbb3cd949968f18be7.png

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

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.

解决方案

You should change 1 MONTH to 30 DAY:

WHERE start_date > NOW() - INTERVAL 30 DAY

To limit it to 30 days in either direction:

WHERE start_date > NOW() - INTERVAL 30 DAY

AND start_date < NOW() + INTERVAL 30 DAY

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值