How to query the intersection of time periods in Mysql

How to query the intersection of time periods in Mysql

Mysql query time period intersection

Usage scenarios

The database table has two fields starttime and endtime. Now given the time period (a, b), find the data that intersects with the time period (starttime, endtime).

sql

select * from TABLENAME where  
    (starttime > a AND starttime < b) OR 
    (starttime < a AND endtime > b) OR
    (endtime > a AND endtime < b) OR
    (starttime = a AND endtime = b);

Mysql query whether two time periods intersect

Database fields start_time, end_time

Input fields a,b

The first

SELECT * FROM test_table
WHERE
    (start_time >= a AND start_time <= b)
    OR (start_time <= a AND end_time >= b)
    OR (end_time >= a AND end_time <= b)

The second

SELECT * FROM test_table
WHERE
    NOT (
        (end_time < a
        OR (start_time > b)
    )

Both results are the same.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Summary of four situations of joint query between two tables in Mysql
  • How to query records between two dates in MySQL
  • MySql query time period method
  • MySql method to query data by time period (example description)

<<:  Using Zabbix to monitor the operation process of Oracle table space

>>:  Detailed explanation of the use of this.$set in Vue

Recommend

How to start multiple MySQL instances in CentOS 7.0 (mysql-5.7.21)

Configuration Instructions Linux system: CentOS-7...

Vue+Websocket simply implements the chat function

This article shares the specific code of Vue+Webs...

How to modify mysql to allow remote connections

Regarding the issue of MySQL remote connection, w...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

How to redraw Button as a circle in XAML

When using XAML layout, sometimes in order to make...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

Explore the truth behind the reload process in Nginx

Today's article mainly introduces the reload ...

Prevent HTML and JSP pages from being cached and re-fetched from the web server

After the user logs out, if the back button on the...

How to connect to MySQL remotely through Navicat

Using Navicat directly to connect via IP will rep...

Summary of Common Commands for Getting Started with MySQL Database Basics

This article uses examples to describe the common...