mysql查询时间段存在重合,MySQL检查两个日期范围是否与输入重叠

I need to check if two dates over lap with another two dates in my database.

My database looks like this

+----+--------------+------------+------------+

| id | code | StartDate | EndDate |

+----+--------------+------------+------------+

| 1 | KUVX-21-40 | 2013-10-23 | 2013-11-22 |

| 2 | UEXA286-1273 | 2013-10-30 | 2013-11-29 |

| 3 | UAJFAU-2817 | 2013-10-21 | 2013-11-20 |

| 4 | KUVX-21-40 | 2013-10-30 | 2013-11-29 |

+----+--------------+------------+------------+

In my query i specify the scope: A start date and an enddate

Lets asign them as follows:

ScopeStartDate = "2013-10-1"

ScopeEndDate = "2013-11-26"

Above should return me all of the records, since the all overlapse the timespan.

However I cannot get a query working :/

I've tried the following query with no luck:

WHERE

(

(StartDate < ScopeStartDate AND StartDate > ScopeStartDate)

OR

(StartDate > ScopeStartDate AND EndDate < ScopeEndDate )

)

This returns me two results:

1 and 3

what am I doing wrong?

解决方案

I believe the following condition matches every possible overlapping case.

WHERE

(

(ScopeStartDate <= EndDate AND ScopeEndDate >= StartDate)

)

except if you declare illogic timespans (for example, those which end before starting)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值