Home >Database >Mysql Tutorial >How to query today's data in mysql
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( ));”.
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( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
Previous month
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =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!