Home > Database > Mysql Tutorial > body text

mysql查询今天、昨天、近7天、近30天、本月、上一月的SQL语句_MySQL

WBOY
Release: 2016-06-01 13:07:10
Original
951 people have browsed it

mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法分析总结:
话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下:
复制代码 代码如下:
select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');

或者:
复制代码 代码如下:
select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());

假设以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查询语句也可按如下写法:
查询今天的信息记录:
复制代码 代码如下:
select * from `article` where to_days(`add_time`) = to_days(now());

查询昨天的信息记录:
复制代码 代码如下:
select * from `article` where to_days(now())

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template