龙空技术网

MySQL判断时间段是否重合

JavaMa 66

前言:

此刻朋友们对“mysql 判断今天”可能比较看重,看官们都想要学习一些“mysql 判断今天”的相关知识。那么小编在网上汇集了一些关于“mysql 判断今天””的相关资讯,希望看官们能喜欢,咱们快快来学习一下吧!

两种写法。

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

第一种写法:

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 判断今天