2012-10-26 119 views
1

我在酒店預訂系統中有三個查詢,其中預訂自動從總房數中扣除。我只想要這三個查詢全部只用一個查詢。我的查詢是這樣的:查詢酒店預訂

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

感謝你們分享你的想法,這個解決方案是你的答案組合..再次感謝!

+0

我想也是這個查詢,但它does not工作。 $ query = mysql_query(「SELECT sum(qty)FROM prereservation where('$ arival'BETWEEN arrival and departure)and('departure'between'$ arival'and'$ departure')and('$ departure'BETWEEN arrival and departure )和room_id ='$ a'AND status ='active'「); – frightnight

+1

提供表格結構,示例數據和您想要的結果。 – Laurence

+0

什麼是'arival'?這是一個錯誤的到來,或者你在做一些奇特的命名方案嗎? – kush

回答

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_職能are discouraged,你會被做得很好遷移到PDO

舊API不應該使用,並且有一天它將被棄用,並最終從PHP中刪除。這是一個受歡迎的擴展,因此這個 將是一個緩慢的過程,但強烈建議您將所有 新代碼寫入mysqliPDO_MySQL

+0

當你的輸入是$ arrival = 29/10/2012和$ departure = 29/10/2012,bu1和bu3被扣除時,還有另一個衝突,我該如何解決這個問題?請幫忙。謝謝。 – frightnight

+0

最簡單的方法是添加一個異常:'if($ arrival == $ departure){$ inogbuwin ++; $ inogbuwin3 ++; }'。 – LSerni