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

Detailed explanation of the problem when combining CSS ellipsis and padding

Text truncation with CSS Consider the following c...

Vue implements page caching function

This article example shares the specific code of ...

In-depth understanding of the seven communication methods of Vue components

Table of contents 1. props/$emit Introduction Cod...

Enterprise-level installation tutorial using LAMP source code

Table of contents LAMP architecture 1.Lamp Introd...

Mysql database index interview questions (basic programmer skills)

Table of contents introduction Indexing principle...

Alibaba Cloud Server Tomcat cannot be accessed

Table of contents 1. Introduction 2. Solution 2.1...

onfocus="this.blur()" is hated by blind webmasters

When talking about the screen reading software op...

How to deploy MySQL 5.7 & 8.0 master-slave cluster using Docker

> Deploy MySQL 5.7 cluster master & slave ...

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

Which scenarios in JavaScript cannot use arrow functions

Table of contents 1. Define object methods 2. Def...

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

Small problem with the spacing between label and input in Google Browser

Code first, then text Copy code The code is as fol...

js to call the network camera and handle common errors

Recently, due to business reasons, I need to acce...

JavaScript canvas realizes dynamic point and line effect

This article shares the specific code for JavaScr...