Home  >  Article  >  Database  >  How to query data within 30 days in mysql

How to query data within 30 days in mysql

王林
王林Original
2020-09-29 14:36:527243browse

Mysql method to query data within 30 days: execute the sql statement [SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date (time field name);].

How to query data within 30 days in mysql

Today:

(Recommended tutorial: mysql tutorial)

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

Yesterday:

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1;

7 days ago:

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);

30 days:

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);

This month:

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, &#39;%Y%m&#39; ) = DATE_FORMAT( CURDATE( ) , &#39;%Y%m&#39; );

Related recommendations: php training

The above is the detailed content of How to query data within 30 days in mysql. For more information, please follow other related articles on the PHP Chinese website!

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