通过Mysql查询今天,昨天,近7天,近30天,本月,上一月的数据方法。

查询今天的信息记录:

select * from `article` where to_days(`add_time`) = to_days(now());

查询昨天的信息记录:

select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;

查询近7天的信息记录:

select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);

查询近30天的信息记录:

select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);

查询本月的信息记录:

select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');

查询上一月的信息记录:

select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;

具体案例操作如下:

select str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')
from article
where str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')>='2012-06-28 08:00:00' and str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')<='2012-06-28 09:59:59';

参考文章:http://www.oschina.net/code/snippet_583419_22850