简体   繁体   English

在日期重叠之间使用mysql进行预订

[英]Using mysql between for date overlap for booking purpose

I am using mysql BETWEEN statement to find the overlap between the dates.It returns quite good results. 我正在使用mysql BETWEEN语句查找日期之间的重叠。它返回了很好的结果。 But still there are few problems that I hope can be resolved.Thanks. 但我仍然希望解决一些问题。谢谢。

This is my database for testing 这是我要测试的数据库

booking_id |venue_id |startdate   | enddate
1001       |3        | 2017-07-21 |2017-07-23

This is my query; 这是我的查询; my concept is either one of the date hit the query so it will return true.so i am using OR to test them. 我的概念是命中查询的日期之一,因此它将返回true。所以我正在使用OR进行测试。

SELECT * FROM `booking` WHERE ('user_inputdate_1' BETWEEN startdate AND enddate) OR ('user_inputdate_2' BETWEEN startdate AND enddate)'

My testing result 我的测试结果

user_inputdate_1  user_inputdate_2  Result 
2017-07-19        2017-07-23         true 
2017-07-22        2017-07-24         true 
2017-07-21        2017-07-24         true 
2017-07-20        2017-07-24         false => this is the problem (it should be true)

PS: If there any other ways to solve this, I will accept them. PS:如果还有其他解决方法,我会接受的。 I have been trying lot of queries but they did not work for me. 我一直在尝试很多查询,但是它们对我没有用。

Lastly,thanks for all your effort. 最后,感谢您的努力。

For the given input dates: 对于给定的输入日期:

2017-07-20 & 2017-07-24 2017-07-202017-07-24

neither start date is greater or equal to 2017-07-21 and end date is also not less than or equal to 2017-07-23 . start date均不得大于或等于2017-07-21end date也不得小于或等于2017-07-23 That's why the result false , which is right. 这就是为什么结果为false ,这是正确的。

So instead of between , try: 因此,与其between ,尝试:

WHERE 'user_inputdate_1' <= `enddate`
OR 'user_inputdate_2' => `startdate`;

Edited: Missing equals to = sign 编辑:缺少等于=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM