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