简体   繁体   English

仅在 MySQL 中返回最近 3 个月的记录

[英]Returning records from the last 3 months only in MySQL

I have a table with a timestamp field.我有一个带有时间戳字段的表。 How do I get data from the last 3 months?如何获取最近 3 个月的数据?

In particular, March is my current month let say, 03/2012 .特别是,三月是我当前的月份,比如说03/2012 I need to return records from the months March, February, and January only.我只需要返回 3 月、2 月和 1 月的记录。

3 months before today:今天前3个月:

select * from table where timestamp >= now()-interval 3 month;

Start with first of month:从月初开始:

select * from table where timestamp >= last_day(now()) + interval 1 day - interval 3 month;

To get the first day of the current month, you could use this:要获取当月的第一天,您可以使用以下命令:

DATE_FORMAT(CURDATE(), '%Y-%m-01')

if current date is 2013-03-13 , it will return 2013-03-01 , and we can just substract 2 months from this date to obtain 2013-01-01 .如果当前日期是2013-03-13 ,它将返回2013-03-01 ,我们可以从该日期减去 2 个月得到2013-01-01 Your query could be like this:您的查询可能是这样的:

SELECT *
FROM yourtable
WHERE data >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH

Assuming you're using SQL Server (Oracle, MySQL and others have similar date functions), you can use the dateadd function to add or subtract an interval to the current date.假设您使用的是 SQL Server(Oracle、MySQL 和其他有类似的日期函数),您可以使用 dateadd 函数为当前日期添加或减去一个间隔。

If you want a full three months, you can subtract 3 months from today : DATEADD(m,-3,getdate())如果你想要整整三个月,你可以从今天减去 3 个月: DATEADD(m,-3,getdate())

But, as you state, you only want data from January, February and March.但是,正如您所说,您只需要 1 月、2 月和 3 月的数据。 You have to make some calculation based on today's date: dateadd(m,-2, CONVERT(datetime, CONVERT(VARCHAR(2), MONTH(getdate())) + '/01/' + CONVERT(VARCHAR(4), YEAR(getdate()))))您必须根据今天的日期进行一些计算: dateadd(m,-2, CONVERT(datetime, CONVERT(VARCHAR(2), MONTH(getdate())) + '/01/' + CONVERT(VARCHAR(4), YEAR(getdate()))))

And in the end, get a query like最后,得到一个类似的查询

SELECT fields 
FROM table 
WHERE timestampfield > DATEADD(m,-2, CONVERT(datetime, CONVERT(VARCHAR(2), MONTH(getdate()))  + '/01/' + CONVERT(VARCHAR(4), YEAR(getdate()))))

--- edit --- erf, I just noticed the "mysql" tag... you can get more information on MySQL date functions here : https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html --- 编辑 --- erf,我刚刚注意到“mysql”标签...您可以在此处获取有关 MySQL 日期函数的更多信息: https ://dev.mysql.com/doc/refman/5.5/en/date -and-time-functions.html

I know this is an old question, but to possibly save others time and to sum the above answers up for the case of needing (1) dates from current month and (2) dates from the prior 2 months (common when displaying data statistics):我知道这是一个老问题,但为了可能节省其他人的时间并总结上述答案,以应对需要 (1) 本月日期和 (2) 前 2 个月日期的情况(在显示数据统计信息时很常见) :

WHERE ((timestamp >= NOW() - DATE_FORMAT(CURDATE(), '%Y-%m-01'))
OR  (timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH))

另一种可能性是:

SELECT * WHERE your_date_column > LAST_DAY(CURRENT_DATE - INTERVAL 3 MONTH);

Use this code here to get the previous 3 months from a certain date在此处使用此代码获取从某个日期开始的前 3 个月

SELECT * FROM table WHERE date_column>= DATE_FORMAT(current_date(), '%Y-%m-01') - INTERVAL 3 MONTH and date_column< DATE_FORMAT(current_date(), '%Y-%m-01')
WHERE ((timestamp >= NOW() - DATE_FORMAT(CURDATE(), '%Y-%m-01'))
OR  (timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM