need updation for mysql query for choosing date range for reservation of a hotel room or any thing
i had a situation in my project that is as follows.
while checking for the available rooms
$sel_from_bookings="SELECT room_no FROM `booking` WHERE (('".$_POST['req_tdate']."' BETWEEN check_indate AND check_outdate) OR ('".$_POST['req_fdate']."' BETWEEN check_indate AND check_outdate)";
$sel_from开发者_如何转开发_reserv="SELECT room_no FROM `reservation` WHERE (('".$_POST['req_tdate']."' BETWEEN check_indate AND check_outdate) OR ('".$_POST['req_fdate']."' BETWEEN check_indate AND check_outdate))";
$sel_rooms="SELECT room_no FROM rooms WHERE room_no NOT IN (".$sel_from_bookings.") AND room_no NOT IN (".$sel_from_reserv.")";
The first query retrives the list of room numbers from the booking table which satisfies the daterange
similarly the second one dos same from the table reservation
the last query uses the list provided by the above two queries and gets the list of room which are not in the generated list.
works fine for 10-08-2010 / 15-08-2010
works fine for 20-08-2010 / 25-08-2010
when i give the dates between 10 and 15 it works fine similarly for 20 and 25 and also works fine for the dates 14-08-2010 and 21-08-2010
but not working for 16-08-2010 to 19-08-2010
need any clarification please ask me.
Thanks.
SELECT *
FROM room
WHERE room_no NOT IN
(
SELECT room_no
FROM booking
WHERE check_outdate >= @req_fdate
AND check_indate <= @red_tdate
)
AND room_no NOT IN
(
SELECT room_no
FROM reservation
WHERE check_outdate >= @req_fdate
AND check_indate <= @red_tdate
)
Pay attention to the order or the arguments: @req_fdate
here is the first date here (from), @req_tdate
is the last date (till).
To check for availability from Aug 16
to Aug 19
, use this:
SELECT *
FROM room
WHERE room_no NOT IN
(
SELECT room_no
FROM booking
WHERE check_outdate >= '2010-08-16'
AND check_indate <= '2010-08-19'
)
AND room_no NOT IN
(
SELECT room_no
FROM reservation
WHERE check_outdate >= '2010-08-16'
AND check_indate <= '2010-08-19'
)
what datatype do the fields check_indate and check_outdate have?
my guess is that the BETWEEN keyword is not working as expected. Do things work differently if you replace a BETWEEN b and c
with a >= b and a <= c
?
$sel_from_bookings="SELECT room_no FROM `booking` WHERE (('".$_POST['req_tdate']."' >= check_indate AND '".$_POST['req_tdate']."' <= check_outdate) OR ('".$_POST['req_fdate']."' >= check_indate AND '".$_POST['req_fdate']."' <= check_outdate)";
$sel_from_reserv="SELECT room_no FROM `reservation` WHERE (('".$_POST['req_tdate']."' >= check_indate AND '".$_POST['req_tdate']."' <= check_outdate) OR ('".$_POST['req_fdate']."' >= check_indate AND '".$_POST['req_fdate']."' <= check_outdate))";
$sel_rooms="SELECT room_no FROM rooms WHERE room_no NOT IN (".$sel_from_bookings.") AND room_no NOT IN (".$sel_from_reserv.")";
ps: you can also try to debug by casting your examples to the datatype of your date columns. if we assume your check_indate column is of type datetime, the see what mysql gives back if you try this:
SELECT CAST('16-08-2010' as datetime);
vs
SELECT CAST('20-08-2010' as datetime);
My mysql here fails on both examples since it expects yyyy-mm-dd as date format, but it might give you a hint :)
I would look at the type of data field you are using for the check_outdate and check_indate.
If you are using 'DATE', make sure the input is in the format YYYY-MM-DD. If you input anything else without separators, then MySQL will second guess the date. You can use the PHP function to convert a date format from DD-MM-YYYY (or whatever) to the correct date expected by MySQL:
$date ='12-12-2007';
$dateTime = new DateTime($date);
$formatted_date=date_format ( $dateTime, 'Y-m-d' );
echo $formatted_date;
// This will output 2007-12-12
If you are using a VARCHAR or fixed length CHAR, then string comparisons may not be as expected.
精彩评论