Home > Database > Mysql Tutorial > body text

How to set time query conditions in mysql

WBOY
Release: 2022-05-19 17:16:56
Original
5395 people have browsed it

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, etc.

How to set time query conditions in mysql

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

How to set time query conditions in mysql

DATE_ADD() function

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

DATE_ADD(date,INTERVAL expr type)
Copy after login

date parameter is a legal date expression. The expr parameter is the time interval you wish to add.

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

DATE_SUB function

DATE_SUB Function subtracts a specified time interval from a date.

DATE_SUB(date,INTERVAL  expr type)
Copy after login

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

The type parameter can be the following values: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR, etc.

DATE_FORMAT() function

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

DATE_FORMAT(date,format)
Copy after login

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 second, %y year 2 digits

TO_DAYS() function

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

FROM_UNIXTIME() function

Convert timestamp to date. Note that the timestamp in mysql is 10 digits.

UNIX_TIMESTAMP() function

Date to timestamp

SELECT NOW() FROM DUAL
SELECT to_days(now()) FROM DUAL
SELECT date_format(now(),'%Y-%m-%d') FROM DUAL
SELECT DATE_SUB(NOW(),INTERVAL 3 DAY) FROM dual
SELECT DATE_ADD(NOW(),INTERVAL 3 DAY) FROM dual
Copy after login

Recommended learning: mysql video tutorial

The above is the detailed content of How to set time query conditions 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!