简体   繁体   English

查询酒店预订

[英]Query for hotel reservation

I have three queries in a hotel reservation system where reserves are automatic deducted from the total number of rooms. 我在酒店预订系统中有三个查询,其中的预订量自动从客房总数中扣除。 All I want those three queries goes all in only one query. 我只希望这三个查询全部都包含在一个查询中。 My query looks like this: 我的查询如下所示:

$p = is in a foreach loop where $p['id'] is the id of each room class and $p['qty'] is the maximum rooms of each room class. $p =foreach循环中,其中$p['id']是每个房间类别的ID ,而$p['qty']是每个房间类别的最大房间。

$a = $p['id'];
$query = mysql_query("SELECT sum(qty) FROM prereservation where '$arrival' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin=$rows['sum(qty)'];
          }
$query = mysql_query("SELECT sum(qty) FROM prereservation where departure BETWEEN '$arrival' and '$departure' and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin2=$rows['sum(qty)'];
          }
$query = mysql_query("SELECT sum(qty) FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin3=$rows['sum(qty)'];
          }

<select>
 <option value="0"></option>
 <? $counter = 1; ?>
 <? while ($counter <= ($p['qty'])-($inogbuwin + $inogbuwin2 + $inogbuwin3)){ ?>
 <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
 <? $counter++;
 }?>
</select>

Every time I put a date between the range of those three queries, the total deducted is also tripled which is the reason I want those three queries to be all in one. 每次我在这三个查询的范围之间放置一个日期时,扣除的总数也会增加三倍,这就是我希望这三个查询合为一体的原因。

for example i have a record in database of arrival = 27/10/2012 and departure = 29/10/2012 例如我在arrival = 27/10/2012 and departure = 29/10/2012数据库中有一条记录

and the input dates that are not available in range is 并且输入日期在范围内不可用

 $arrival = 27/10/2012 and $departure = 29/10/2012 
 $arrival = 26/10/2012 and $departure = 27/10/2012 
 $arrival = 29/10/2012 and $departure = 30/10/2012 
 $arrival = 26/10/2012 and $departure = 30/10/2012 
 $arrival = 29/10/2012 and $departure = 29/10/2012 

so each date that in rance of those three queries are also the deductions, so i want those queries to be in one. 因此,在这三个查询中,每个日期也是扣除额,因此我希望这些查询合而为一。 thanks guys 多谢你们

after all i fixed the errors but only one preblem left. 毕竟我修复了错误,但只剩下一个问题。 i fixed the reservation dates in only a single month, the problem is when the inputs of arrival and departure are not in the same month it does'nt work either. 我只在一个月内确定了预订日期,问题是当到达和离开的输入不在同一个月时,它也不起作用。 here's my code again below. 这是我的代码再次在下面。

<?
$a = $p['id'];
    $query = mysql_query("SELECT
    SUM(IF('$arrival' BETWEEN arrival and departure, qty, 0)) AS bu1,
    SUM(IF('$departure' BETWEEN arrival and departure, qty, 0)) AS bu2,
    SUM(IF(arrival > '$arrival' and departure < '$departure', qty, 0)) AS bu3
    FROM prereservation WHERE room_id ='$a' and status = 'active'");
    $row = mysql_fetch_array($query);

    $test1 = $row['bu1'];
    if ($row['bu1'] == $row['bu2']){
        $test2 = $row['bu2'] - $row['bu1'];
    }else{
        $test2 = $row['bu2'];
    }       
        $test3 = $row['bu3'];
    ?>      
<select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
<option value="0"></option>
    <? $counter = 1; ?>
<? while ($counter <= ($p['qty']) - ($test1 + $test2 + $test3)){ ?>
    <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
    <? $counter++;
    }?>

please help me guys how to solve this reservation query, or maybe there's an other way to solve this in php code. 请帮助我如何解决此保留查询,或者也许还有另一种方法可以在php代码中解决此问题。 thanks guys. 多谢你们。

Finally i'm finished and found the solution, here it is: 最后,我完成并找到了解决方案,这里是:

            $a = $p['id'];
        $query1 = mysql_query("SELECT DISTINCT id, SUM(qty)
        FROM prereservation 
        WHERE 
        (
            ( '$arival1' BETWEEN arrival AND departure ) OR 
            ( '$departure1' BETWEEN arrival AND departure ) OR 
            ( arrival > '$arival1' AND departure < '$departure1' )
        )
            AND room_id ='$a' 
            AND STATUS = 'active'");  
        while($rows1 = mysql_fetch_assoc($query1)){
        $set1 = $rows1['SUM(qty)'];
        }   
        ?> 
        <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
        <option value="0"></option>
        <? $counter = 1; ?>
        <? while ($counter <= ($p['qty']) - $set1){ ?>
        <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
        <? $counter++;
        }?>
        </select>

thank you guys for sharing your ideas, this solution is the combinations of your answers.. thanks again!! 感谢你们分享您的想法,这个解决方案是您的答案的组合..再次感谢!

You could do it like this: 您可以这样做:

SELECT
    SUM(IF('$arrival'    BETWEEN arrival   and departure,    qty, 0)) AS bu1,
    SUM(IF(departure    BETWEEN '$arrival' and '$departure', qty, 0)) AS bu2,
    SUM(IF('$departure' BETWEEN arrival   and departure,    qty, 0)) AS bu3
FROM prereservation WHERE room_id ='$a' and status = 'active'"

and then in PHP: 然后在PHP中:

$query = mysql_query(...);
$row = mysql_fetch_array($query);
$inogbuwin =$row['bu1'];
$inogbuwin2=$row['bu2'];
$inogbuwin3=$row['bu3'];
mysql_free($query);

include the customary warning that mysql_ functions are discouraged and you would do well by migrating to PDO 包括 不鼓励使用 mysql_函数 的常规警告, 您可以通过迁移到PDO来做得很好

The old API should not be used, and one day it will be deprecated and eventually removed from PHP . 不应使用旧的API,并且有一天将不推荐使用它,最终将其从PHP中删除 It is a popular extension so this will be a slow process, but you are strongly encouraged to write all new code with either mysqli or PDO_MySQL . 这是一个流行的扩展,因此这将是一个缓慢的过程,但是强烈建议您使用mysqliPDO_MySQL编写所有新代码。

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

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