简体   繁体   English

需要更新mysql查询,以选择预订酒店房间或任何东西的日期范围

[英]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 适用于10-08-2010 / 15-08-2010

works fine for 20-08-2010 / 25-08-2010 适用于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 当我给出10到15之间的日期时,它同样适用于20和25,并且在14-08-2010和21-08-2010的日期也可以正常工作

but not working for 16-08-2010 to 19-08-2010 但不适用于16-08-2010至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 ). 注意顺序或参数: @req_fdate这里是第一个日期( ), @req_tdate是最后一个日期( 直到 )。

To check for availability from Aug 16 to Aug 19 , use this: 要查看Aug 16Aug 19空房情况,请使用:

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? check_indate和check_outdate字段有哪些数据类型?

my guess is that the BETWEEN keyword is not working as expected. 我的猜测是BETWEEN关键字没有按预期工作。 Do things work differently if you replace a BETWEEN b and c with a >= b and a <= c ? 如果用a >= b and a <= c替换a BETWEEN b and 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. ps:您也可以通过将示例转换为日期列的数据类型来尝试调试。 if we assume your check_indate column is of type datetime, the see what mysql gives back if you try this: 如果我们假设你的check_indate列的类型是datetime,那么如果你试试这个,请看看mysql给出了什么:

SELECT CAST('16-08-2010' as datetime);

vs 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 :) 我的mysql在这两个例子都失败了,因为它期望yyyy-mm-dd作为日期格式,但它可能会给你一个提示:)

I would look at the type of data field you are using for the check_outdate and check_indate. 我将查看您用于check_outdate和check_indate的数据字段的类型。

If you are using 'DATE', make sure the input is in the format YYYY-MM-DD. 如果您使用'DATE',请确保输入的格式为YYYY-MM-DD。 If you input anything else without separators, then MySQL will second guess the date. 如果你输入任何没有分隔符的东西,那么MySQL会猜测日期。 You can use the PHP function to convert a date format from DD-MM-YYYY (or whatever) to the correct date expected by MySQL: 您可以使用PHP函数将日期格式从DD-MM-YYYY(或其他)转换为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. 如果您使用VARCHAR或固定长度CHAR,则字符串比较可能不是预期的。

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

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