如何使用MySQL从新闻表中选择最近6个月

时间:2021-10-27 09:26:18

I am trying to select the last 6 months of entries in a table, I have a column called datetime and this is in a datetime mysql format.

我试图在表中选择最近6个月的条目,我有一个名为datetime的列,这是一个日期时间的mysql格式。

I have seen many ways using interval and other methods - which method should I use? Thanks

我已经看到很多使用区间和其他方法的方法 - 我应该使用哪种方法?谢谢

3 个解决方案

#1


88  

Use DATE_SUB

使用DATE_SUB

 .... where yourdate_column > DATE_SUB(now(), INTERVAL 6 MONTH)

#2


11  

Try this:

尝试这个:

select *
  from table 
 where your_dt_field >= date_sub(now(), interval 6 month);

Query reads: give me all entries in table where the field corresponding to the entry date is newer than 6 months.

查询读取:给我表中的所有条目,其中对应于输入日期的字段小于6个月。

#3


1  

I tried @user319198 answer to display last 6 months (sum of) sales, it worked but I faced one issue in the oldest month, i do not get the sales amount of the whole month. The result starts from the equivalent current day of that month.

我试过@ user319198答案显示最近6个月(总和)销售,它工作但我在最老的月份面临一个问题,我没有得到整个月的销售额。结果从该月的当天当天开始。

Just I want to share my solution if any one interested:-

如果有兴趣的话,我想分享我的解决方案: -

yourdate_column > DATE_SUB(now(), INTERVAL 7 MONTH)
limit 6

Also it will be great if anyone has better solution for my case J.

如果有人对我的案例J有更好的解决方案,那也很棒。

#1


88  

Use DATE_SUB

使用DATE_SUB

 .... where yourdate_column > DATE_SUB(now(), INTERVAL 6 MONTH)

#2


11  

Try this:

尝试这个:

select *
  from table 
 where your_dt_field >= date_sub(now(), interval 6 month);

Query reads: give me all entries in table where the field corresponding to the entry date is newer than 6 months.

查询读取:给我表中的所有条目,其中对应于输入日期的字段小于6个月。

#3


1  

I tried @user319198 answer to display last 6 months (sum of) sales, it worked but I faced one issue in the oldest month, i do not get the sales amount of the whole month. The result starts from the equivalent current day of that month.

我试过@ user319198答案显示最近6个月(总和)销售,它工作但我在最老的月份面临一个问题,我没有得到整个月的销售额。结果从该月的当天当天开始。

Just I want to share my solution if any one interested:-

如果有兴趣的话,我想分享我的解决方案: -

yourdate_column > DATE_SUB(now(), INTERVAL 7 MONTH)
limit 6

Also it will be great if anyone has better solution for my case J.

如果有人对我的案例J有更好的解决方案,那也很棒。