根据日期范围过滤列

时间:2016-07-21 14:58:19

标签: mysql sql date select where

我需要按日期范围过滤包含时间和日期的列。 我只是想看看从现在起一个月内记录的价值。(2016-06-21至2016-07-21) 列中的日期格式为YYYY-mm-dd HH:mm:ss 该列中的一些数据如下所示:

2016-07-01 13:38:04
2016-07-01 12:52:41
2016-07-01 12:52:41
2015-05-30 13:04:42
2015-05-30 13:04:42
2016-06-29 10:23:39
2016-06-29 10:23:39
2016-06-29 09:49:43
2015-05-29 09:49:20
2015-05-28 15:05:11
2016-05-28 15:04:46
2016-06-28 15:00:43
2016-05-30 11:33:42
2016-05-30 11:30:53
2015-05-02 09:54:34

2 个答案:

答案 0 :(得分:0)

你可以这样做:

select t.*
from t
where col >= date_sub(curdate(), interval 1 month);

无论您的列是以字符串还是datetime存储,这都应该有效,因为格式很容易转换为datetime。但是,您应该使用本机类型作为日期和时间,而不是字符串。

答案 1 :(得分:0)

在某种程度上你可以使用: -

SELECT * 
 FROM drange_table 
 Where date_range_column between DATE_SUB(curdate(), interval 1 month) and curdate()