php mysql上个月日期语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1479745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql last month date statement
提问by An employee
I have a DATE, how do I write a wherethat checks for last month until now (either the first of or just today's day -1 month)?
我有一个日期,我如何写一个where检查上个月到现在(第一天或今天 -1 个月)的支票?
回答by andri
SELECT * FROM table WHERE dateField > DATE_SUB(NOW(), INTERVAL 1 MONTH)
This selects all rows that have a dateFieldmore recent than current timestamp minus one month (so, for example, given today is 26 Sep 2009 00:56 it would give rows that are more recent than 26 Aug 2009 00:56)
这将选择dateField比当前时间戳减去一个月的所有行(例如,假设今天是 2009 年 9 月 26 日 00:56,它将给出比 2009 年 8 月 26 日 00:56 更近的行)
回答by mimoralea
Actually, last month until now would be:
实际上,从上个月到现在将是:
From the beginning of the month:
从月初开始:
SELECT * FROM table
WHERE date >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m-01')
AND date <= NOW()
Since a month ago: (hopefully you have no future dates on the table)
从一个月前开始:(希望你没有未来的日期)
SELECT * FROM table
WHERE date >= (CURRENT_DATE - INTERVAL 1 MONTH)
Alternatively, you might want only the last month... Say today is September and you want all August, not including any of September... That is what I understand as 'last month'.
或者,您可能只想要最后一个月...假设今天是九月,而您想要整个八月,不包括九月的任何一天...这就是我所理解的“上个月”。
SELECT * FROM table
WHERE date >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01')
AND date < DATE_FORMAT(CURRENT_DATE, '%Y/%m/01')
You could also do for the same result:
您也可以为相同的结果做:
SELECT * FROM table
WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)

