Home  >  Article  >  Database  >  How to query today's data in mysql

How to query today's data in mysql

WBOY
WBOYOriginal
2022-01-18 16:38:1510947browse

In mysql, you can use the select statement with the "to_days()" and now() functions to query today's data. The syntax is "select * from table name where to_days (time field name) = to_days(now( ));”.

How to query today's data in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How does mysql query today's data

mysql If you want to query today's data, you can use the SELECT statement with the TO_DAYS function and now function to query today The data.

MySQL database uses the SQL SELECT statement to query data.

TO_DAYS(date)

Given a date, return a date number (the number of days since year 0).

NOW() Returns the current date and time.

Syntax

NOW()

The syntax for querying today’s data is:

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

Extension:

Yesterday

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

Last 7 days

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

Nearly 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; )

Previous month

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , &#39;%Y%m&#39; ) , date_format( 时间字段名, &#39;%Y%m&#39; ) ) =1

Recommended learning: mysql video tutorial

The above is the detailed content of How to query today's data 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