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

How to query today's data in mysql

WBOY
Release: 2022-01-18 16:38:15
Original
11032 people have browsed it

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)
Copy after login

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

NOW() Returns the current date and time.

Syntax

NOW()
Copy after login

The syntax for querying today’s data is:

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

Extension:

Yesterday

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

Last 7 days

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

Nearly 30 days

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

This month

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

Previous month

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

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!

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