Home >Database >Mysql Tutorial >mysql查询今天、昨天、7天、近30天、本月、上一月 数据_MySQL

mysql查询今天、昨天、7天、近30天、本月、上一月 数据_MySQL

WBOY
WBOYOriginal
2016-06-01 13:45:161186browse

bitsCN.com

查询

今天

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

作者“adpit
 

bitsCN.com
Statement:
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