两种写法。如图,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判断时间段是否重合的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。