Lukas Klizas Lukas Klizas - 1 year ago 113
MySQL Question

How can I check for date range exists

As you may have noticed from title, my question is quite difficult to express, but there is problem:

I have MySQL table with some data:

enter image description here

In registration form I have to specify reservation start and end times. How can I check, whether there is already registered reservation for specified time or not. For example I should not be able to put new reservation which starts at 13:20 and ends at 15:00 etc.

Answer Source

If either the start or the end of the new reservation you want to create is inside a current range, the reservation should be blocked. Using :reservationStart and :reservationEnd as these dates, you could query:

FROM   reservations
WHERE  :reservationStart BETWEEN reservation_from AND reservation_to OR
       :reservationEnd BETWEEN reservation_from AND reservation_to OR
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download