查询酒店预订

时间:2012-10-26 15:08:49

标签: php mysql sql date

我在酒店预订系统中有三个查询,其中储备会从房间总数中自动扣除。我希望这三个查询只在一个查询中完成。我的查询如下:

$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>

每次我在这三个查询的范围之间放置一个日期,扣除的总数也会增加三倍,这就是我希望这三个查询都在一个中的原因。

例如我在数据库中有记录 arrival = 27/10/2012 and departure = 29/10/2012

并且范围内不可用的输入日期为

 $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 

因此,这三个查询的每个日期也是扣除,所以我希望这些查询在一个中。谢谢你们

毕竟我修复了错误,但只留下了一个问题。我只在一个月内确定了预订日期,问题是当到达和离开的输入不是在同一个月也不起作用的时候。这是我下面的代码。

<?
$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++;
    }?>

请帮助我们如何解决这个预订查询,或者在PHP代码中有另一种方法来解决这个问题。谢谢你们。

最后我已经完成并找到了解决方案,现在是:

            $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>

谢谢你们分享你们的想法,这个解决方案就是你们答案的组合......再次感谢!!

1 个答案:

答案 0 :(得分:1)

你可以这样做:

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'"

然后在PHP中:

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

包含 mysql_ functions are discouraged 的惯例警告,您可以迁移到PDO < / p>

  

不应使用旧的API,有一天将弃用并最终从PHP中移除。这是一个受欢迎的扩展   将是一个缓慢的过程,但强烈建议你写所有   包含mysqliPDO_MySQL的新代码。