MySQL判断时间段是否重合

时间:2021-04-11 21:33:30   收藏:0   阅读:33

两种写法。如图,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);

  

 

原文:https://www.cnblogs.com/chenyablog/p/14644706.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!