简体   繁体   中英

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. 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.

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. 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

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 . That's why the result false , which is right.

So instead of between , try:

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

Edited: Missing equals to = sign

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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