Home > Database > Mysql Tutorial > How to query records for a period of time in mysql

How to query records for a period of time in mysql

coldplay.xixi
Release: 2020-11-18 09:46:40
Original
4253 people have browsed it

Mysql method of querying records for a period of time: 1. Query records within N days, the code is [WHERE TO_DAYS(NOW()) - TO_DAYS (time field) <= N]; 2. Query today’s records, The code is [where date (time field) = date (now ())].

How to query records for a period of time in mysql

More related free learning recommendations: mysql tutorial(video)

mysql method of querying records for a period of time:

Records within 24 hours (i.e. 86400 seconds)

f621a33f4241e97f801a8f24b4be5879

PERIOD_DIFF(P1,P2)

Returns the number of months between periods P1 and P2. P1 and P2 should be specified in YYMM or YYYYMM. Note that the period parameters P1 and P2 are not date values:

mysql> SELECT PERIOD_DIFF(9802,199703);

-> 11

DATE_ADD(date,INTERVAL expr type )

DATE_SUB(date,INTERVAL expr type)

ADDDATE(date,INTERVAL expr type)

SUBDATE(date,INTERVAL expr type)

These functions perform arithmetic operations on dates. ADDDATE() and SUBDATE() are synonyms for DATE_ADD() and DATE_SUB() respectively. In MySQL 3.23, if the right side of the expression is a date value or a datetime field, you can use and - instead of DATE_ADD() and DATE_SUB() (example below). The date parameter is a DATETIME or DATE value specifying the beginning of a date. expr is an expression that specifies whether to add or subtract the interval value from the start date. expr is a string; it can be preceded by a "-" to represent a negative interval value. type is a keyword that indicates the format in which the expression is interpreted.

The above is the detailed content of How to query records for a period of time 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