Get the App
SLTechnology News&Howtos  ›  Database  › 

How to query the data of a year by mysql

Shulou Source: shulou.com Published: 2022-05-31 10:43:03 09月13日 Update

This article mainly introduces mysql how to query the year's data related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this mysql how to query the data of the year, let's take a look.

In mysql, you can use the "DATE_SUB ()" function to query the data for a year, which is used to set the 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.

How does mysql query the data for a year?

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

Grammar

DATE_SUB (date,INTERVAL expr type)

The date parameter is a valid date expression. The expr parameter is the interval you want to add.

Examples are as follows:

SELECT * FROM table name WHERE time field > DATE_SUB (CURDATE (), INTERVAL 1 YEAR) one year SELECT * FROM table name WHERE time field > DATE_SUB (CURDATE (), INTERVAL 1 WEEK) one week SELECT * FROM table name WHERE time field > DATE_SUB (CURDATE (), INTERVAL 3 MONTH) three months

Expand knowledge:

Suppose we have the following "Orders" table:

Now, we want to subtract five days from "OrderDate".

We use the following SELECT statement:

SELECT OrderId,DATE_SUB (OrderDate,INTERVAL 5 DAY) AS SubtractDateFROM Orders

Results:

This is the end of the article on "how to query the data of the year by mysql". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to query the data of mysql for a year". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Time data query field knowledge function date content parameter article grammar legal three value easy to operate tutorial article easy to understand more version Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Microsoft Docker NVidia