MySQL数据库如何判断时间段是否重合的两种方法

两种写法。如图,4种重合情况和2种不重合情况。

第一种写法:

— 时间段 a,b
SELECT * FROM table WHERE
(start_time >= a and end_time <= b) — 被包含了
or (end_time >= a and end_time <=b)
or (start_time >= a and start_time <=b)
or (start_time <= a and end_time >=b)

解析:where后的4个条件分别代表了图中4种重合的情况。
但是第一种情况被2和3包含了,所以简化一下写法:

SELECT * FROM table WHERE
(end_time >= a and end_time <=b)
or (start_time >= a and start_time <=b)
or (start_time <= a and end_time >=b);

第二种写法:

SELECT * FROM table WHERE not (start_time > b or end_time < a);

到此这篇关于MySQL判断时间段是否重合的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
沟通购买:QQ咨询 淘宝咨询 微信咨询 淘宝店铺
版权申明及联系
本站部分文章参考或来源于网络,如有侵权请联系站长。本站提供相关远程技术服务,有需要可联系QQ
数据库远程运维 » MySQL数据库如何判断时间段是否重合的两种方法