Home > Database > Mysql Tutorial > body text

How to query data for one year in mysql

WBOY
Release: 2022-05-16 16:07:37
Original
9498 people have browsed it

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)".

How to query data for one year in mysql

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

How to query one year's data in mysql

DATE_SUB() function subtracts the specified time interval from the date.

Syntax

DATE_SUB(date,INTERVAL expr type)
Copy after login

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

Extended knowledge:

Suppose we have the following "Orders" table:

How to query data for one year in mysql

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

Result:

How to query data for one year in mysql

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!

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