Query the data of the day before the current time interval in MySQL

Query the data of the day before the current time interval in MySQL

1. Background

In actual projects, we will encounter the situation of distributed scheduled task execution. Sometimes when the scheduled task is executed, if the amount of queried data is relatively large, we will choose to execute the data filtered a few days ago before the execution time interval.

Avoid executing the full amount of data, which may cause excessive memory usage or long execution time, leading to OOM and other problems.

Here we will use SQL to filter queries with a time interval of 1 day or several days.

1.1. Query SQL statement

Here we use a talk_course table as the execution form of the query SQL statement. The fields of the table are as follows:

The table contains the following test data:

The SQL statement to query the day before the current execution time is as follows:

# Query data from 1 day before the current time SELECT * FROM `talk_course` t
WHERE t.createTime <= NOW() 
AND t.createTime >= date_sub(now(), interval 1 day);

Query results:

Query the time one day before the current time:

# Query the time of the day before the current time SELECT date_sub(now(),interval 1 day) as yesterday

1.2. Query statement analysis

Two functions are used here:

DATE_ADD(date,INTERVAL expr unit)
DATE_SUB(date,INTERVAL expr unit)

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

The DATE_SUB() function subtracts a specified time interval from a date.

where the expr argument is the time interval you wish to add.

The type parameter can have the following values:

Type value
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

This is the end of this article about querying data one day before the current time interval in MySQL. For more relevant MySQL query data one day before the current time, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • PHP, MySQL query today, query this week, query this month's data instance (the field is a timestamp)
  • MySql method to query data by time period (example description)

<<:  HTML page native VIDEO tag hides the download button function

>>:  Detailed explanation of how to create an array in JavaScript

Recommend

A brief discussion on JavaScript shallow copy and deep copy

Table of contents 1. Direct assignment 2. Shallow...

Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

In the front-end and back-end separation developm...

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

HTML tutorial, understanding the optgroup element

Select the category selection. After testing, IE ...

Table setting background image cannot be 100% displayed solution

The following situations were discovered during d...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

Summary of essential knowledge points for MySQL query optimization

Preface Query optimization is not something that ...

How to remove spaces or specified characters in a string in Shell

There are many methods on the Internet that, alth...

Problems encountered when updating the auto-increment primary key id in Mysql

Table of contents Why update the auto-increment i...

CnBlogs custom blog style sharing

After spending half the night on it, I finally ma...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

Detailed explanation of the usage of MySQL data type DECIMAL

MySQL DECIMAL data type is used to store exact nu...

Detailed explanation of using INS and DEL to mark document changes

ins and del were introduced in HTML 4.0 to help au...

Let's talk about the v-on parameter problem in Vue

Use of v-on:clock in Vue I'm currently learni...