MySQL选择过去3个月的记录 [英] MySQL select records for the past 3 months
本文介绍了MySQL选择过去3个月的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果表中包含以下列,则过去3个月中从表中选择所有行的最明智的方法是什么:
what will be the smartest way to select all rows from MySQL table for the past 3 months if the table has the following columns:
| id (int) | year (int)| month (int) |
请考虑,如果当前月份&例如 2.2016 我需要选择11.2015& 2015年12月1.2016
Considering that if the current month & year are for example 2.2016 I need to select all records for 11.2015 & 12.2015 & 1.2016
如果当前月份大于3很容易,因为我需要选择的所有月份都在同一年,因此我可以从当前月份中减去3并运行简单查询
It is easy if the current month is greater than 3 because all months that I need to select are in the same year so I can subtract 3 from the current month and run simple query
SELECT * FROM mytabe where year=2016 and month >= xx
推荐答案
Select * from mytable where STR_TO_DATE(concat(year,"-",month,"-01"),'%Y-%m-%d')>date_sub(curdate(),Interval 3 month) ;
上述查询将从今天前三个月的日期获取年份和月份
The above query will get fetch year and month from date 3 months before today
这篇关于MySQL选择过去3个月的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文