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

A brief discussion on using Vue to complete the mobile apk project

Table of contents Basic Configuration Entry file ...

Element dynamic routing breadcrumbs implementation example

To master: localStorage, component encapsulation ...

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

HTML pop-up div is very useful to realize mobile centering

Copy code The code is as follows: <!DOCTYPE ht...

1 minute Vue implements right-click menu

Table of contents Rendering Install Code Implemen...

Study notes to write the first program of Vue

Table of contents 1. Write an HTML, the first Vue...

Detailed explanation of mysql download and installation process

1: Download MySql Official website download addre...

JavaScript to achieve custom scroll bar effect

In actual projects, the up and down scroll bars a...

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...

WeChat Mini Program video barrage position random

This article shares the specific code for randomi...

Detailed explanation of the execution order of JavaScript Alert function

Table of contents question analyze solve Replace ...

How to set up virtual directories and configure virtual paths in Tomcat 7.0

Tomcat7.0 sets virtual directory (1) Currently, o...