I am writing a hotel booking system. after lots of studying (including stack overflow) i wrote this sql to find out free rooms:
SELECT
*
FROM room
WHERE
room_id NOT IN (
SELECT room_id
FROM bookings
WHERE
checkin <= '$check_in'
AND checkout >= '$check_out'
)
but the problem is its not considering the time checking is 12:00:00 and checkout is 11:59:00
also its not giving the right queries like within the date range its not working like if I book from 15-18 a single room who’s number is 501. if I again run a query 17-19 this rooms seems free but in reality it should be occupied.
can anyone suggest a very good and effective sql which will get the accurate date so that no clash will happen booking system because the system will be implemented in real so mistakes will cause many issues.
thanks in advance
The problem you’re having is that your query is not sufficiently robust. When you break down the problem, what you have is this:
If the range defined by
$check_inand$check_outoverlaps the range defined bycheckinandcheckoutin any way, then the room is booked. Otherwise, it is free.This means that:
$check_in>=checkinand$check_in<=checkout, the room is BOOKED$check_out>=checkinand$check_out<=checkout, the room is BOOKED$check_in<=checkinand$check_out>=checkout, the room is BOOKEDSo, you need to represent both of these scenarios in your subquery in order to get the information you’re looking for.
Also, you will hopefully be using
datetimefor your comparisons and not justtime, otherwise you will have side effects.EDIT: SQL Query
(Keep in mind that there is more than one way to skin a cat, so to speak. I’m just providing an example that keeps with what you already have as much as possible. Once again, I’m also assuming that
checkin,checkout,$check_in, and$check_outwill all resolve todatetimetypes)