如何从mysql中的当前日期时间减去30天?
SELECT * FROM table
WHERE exec_datetime BETWEEN DATEDIFF(NOW() - 30 days) AND NOW();
如何从mysql中的当前日期时间减去30天?
SELECT * FROM table
WHERE exec_datetime BETWEEN DATEDIFF(NOW() - 30 days) AND NOW();
当前回答
MySQL从现在开始减去天数:
select now(), now() - interval 1 day
打印:
2014-10-08 09:00:56 2014-10-07 09:00:56
其他间隔时间表达式单元参数:
https://dev.mysql.com/doc/refman/5.5/en/expressions.html#temporal-intervals
select now() - interval 1 microsecond
select now() - interval 1 second
select now() - interval 1 minute
select now() - interval 1 hour
select now() - interval 1 day
select now() - interval 1 week
select now() - interval 1 month
select now() - interval 1 year
其他回答
另一种方式
SELECT COUNT(*) FROM tbl_debug WHERE TO_DAYS(`when`) < TO_DAYS(NOW())-30 ;
对于不想使用DATE_SUB的人,使用CURRENT_DATE:
SELECT CURRENT_DATE - INTERVAL 30 DAY
MySQL从现在开始减去天数:
select now(), now() - interval 1 day
打印:
2014-10-08 09:00:56 2014-10-07 09:00:56
其他间隔时间表达式单元参数:
https://dev.mysql.com/doc/refman/5.5/en/expressions.html#temporal-intervals
select now() - interval 1 microsecond
select now() - interval 1 second
select now() - interval 1 minute
select now() - interval 1 hour
select now() - interval 1 day
select now() - interval 1 week
select now() - interval 1 month
select now() - interval 1 year
你也可以使用
select CURDATE()-INTERVAL 30 DAY
SELECT date_format(current_date - INTERVAL 50 DAY,'%d-%b-%Y')
可以在SQL中使用日期格式进行格式化。