In mysql, you can use the "DATE_SUB()" function to query data for one year. This function is used to set a specified time interval subtracted from the date. The syntax is "SELECT * FROM table name WHERE time field> ;DATE_SUB(data, INTERVAL 1 YEAR)".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
DATE_SUB() function subtracts the specified time interval from the date.
Syntax
DATE_SUB(date,INTERVAL expr type)
The date parameter is a legal date expression. The expr parameter is the time interval you wish to add.
The example is as follows:
SELECT * FROM 表名 WHERE 时间字段>DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 一年 SELECT * FROM 表名 WHERE 时间字段>DATE_SUB(CURDATE(), INTERVAL 1 WEEK) 一周 SELECT * FROM 表名 WHERE 时间字段 >DATE_SUB(CURDATE(), INTERVAL 3 MONTH) 三个月
Extended knowledge:
Suppose we have the following "Orders" table:
Now, we want to subtract 5 days from "OrderDate".
We use the following SELECT statement:
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate FROM Orders
Result:
Recommended learning: mysql video tutorial
The above is the detailed content of How to query data for one year in mysql. For more information, please follow other related articles on the PHP Chinese website!