Get the App
SLTechnology News&Howtos  ›  Database  › 

How to set time query condition by mysql

Shulou Source: shulou.com Published: 2022-05-31 10:40:51 11月07日 Update

Most people do not understand the knowledge points of this article "mysql how to set time query conditions", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "mysql how to set time query conditions" article.

Method: 1, use "DATE_ADD (date,INTERVAL expr type)" to add the specified time interval to the date; 2, use "DATE_SUB (date,INTERVAL expr type)" to subtract the specified time interval from the date; 3, use the "TO_DAYS" function, and so on.

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

How to set the time query condition by mysql

DATE_ADD () function

The DATE_ADD () function adds the specified time interval to the date.

DATE_ADD (date,INTERVAL expr type)

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

Type parameters can be the following values: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, etc.

DATE_SUB function

The DATE_SUB function subtracts the specified interval from the date.

DATE_SUB (date,INTERVAL expr type)

The date parameter is the date expression of Hefei. The expr parameter is the interval you want to add.

Type parameters can be the following values: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, etc.

DATE_FORMAT () function

The DATE_FORMAT () function is used to display date / time data in different formats

DATE_FORMAT (date,format)

The date parameter is the date. Format specifies the output format of date / time, which can be the following values:% h hour 01-12,% m month,% s seconds,% y year 2 digits

TO_DAYS () function

Calculate the number of days from the beginning of the year to a given time.

FROM_UNIXTIME () function

Timestamp turns date, notice that the timestamp in mysql is 10 digits.

UNIX_TIMESTAMP () function

Date to time stamp

SELECT NOW () FROM DUALSELECT to_days (now ()) FROM DUALSELECT date_format (now (),'% YMY% MMI% d') FROM DUALSELECT DATE_SUB (NOW (), INTERVAL 3 DAY) FROM dualSELECT DATE_ADD (NOW (), INTERVAL 3 DAY) FROM dual is the content of this article on "how to set time query conditions for mysql". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Tags: Time date function parameter content condition query article format knowledge article expression different legal value most days hours that is tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Apple Shulou Technology Docker