Home > Database > Mysql Tutorial > body text

mysql学习笔记查询今天、昨天、7天、近30天、本月、上一月 数据

WBOY
Release: 2016-06-07 17:52:29
Original
1482 people have browsed it

把一个站长的学习笔记贴出现,这个sql有查询今天、昨天、7天、近30天、本月、上一月 数据语句,有需要的朋友可参考。

查询

今天

 代码如下 复制代码

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

 代码如下 复制代码

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名)

7天

 代码如下 复制代码

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY)

近30天

 代码如下 复制代码

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY)

本月

 代码如下 复制代码

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )

上一月

 代码如下 复制代码

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 时间字段名, ‘%Y%m’ ) ) =1

同时,再附上 一个 mysql官方的相关document

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!