https://blog.csdn.net/qq_33896101/article/details/82012300
总结起来就一句话:最小值小于最大值并且最大值大于最小值,则一定重叠。
我的使用场景: A 表 将时间有交叉的数据查出来
我的算法:start_time,end_time 1> and 2>
1> 剔除完全相同 a1.start_time<>a2.start_time and a1.end_time!=a2.end_time
2> 最小值小于最大值并且最大值大于最小值 a1.start_time<a2.end_time and a1.end_time>a2.start_time
select * from A a1 left join A a2 on (
a1.id=a2.id
)and (
a1.start_time<>a2.start_time and a1.end_time!=a2.end_time
and a1.start_time<a2.end_time and a1.end_time>a2.start_time
)
集合1(第一个判断)
start___________end
a___________b
集合2(第三个判断)
start________________end
a_________b
集合3(第三个判断)
start___________end
a______________b
集合4
start___________end
a_________________________b
集合1并上(or)集合3实际上包含了集合4逻辑
数据库的字段 start_time, end_time
输入的字段 a,b
第一种
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)
第二种
SELECT * FROM test_table
WHERE
NOT (
(end_time < a
OR (start_time > b)
)