2012-11-02 67 views
0

我有3個查詢,我希望所有結果都只有一個使用array_merge和array_unique的結果。在一個結果中合併3個查詢的結果

這裏的疑問:

  $query1 = mysql_query("SELECT * FROM prereservation where '$arival' BETWEEN arrival and departure and room_id ='11' and status = 'active'"); 
     while($rows1 = mysql_fetch_assoc($query1)){ 
     echo $rows1['id']. " - ". $rows1['qty']; 
     echo "<br />"; 
     } 

     $query2 = mysql_query("SELECT * FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='11' and status = 'active'"); 
     while($rows2 = mysql_fetch_assoc($query2)){ 
     echo $rows2['id']. " - ". $rows2['qty']; 
     echo "<br />"; 
     } 

     $query3 = mysql_query("SELECT * FROM prereservation where arrival > '$arival' and departure < '$departure' and room_id ='11' and status = 'active'"); 
     while($rows3 = mysql_fetch_assoc($query3)){ 
     echo $rows3['id']. " - ". $rows3['qty']; 
     echo "<br />"; 
     } 

example result is:  
for  $rows1  |  $rows2  |  $rows3 
     id | qty | id | qty | id | qty 
     -----|--------|-------|--------|-------|------ 
     01 | 1 | 01 | 1 | 02 | 1 
     03 | 1 | 02 | 1 | 03 | 1 
     04 | 1 | 03 | 1 | 04 | 1 
     08 | 1 | 05 | 1 | 05 | 1 

我不知道這是否是正確的?

$sample = array_unique(array_merge($rows1['id'], $rows2['id'], $rows3['id'])) 

我想用array_merge和array_unique每個結果 的ID的所以的ID,將被留在最後的結果是

01, 02, 03, 04, 05, 08. 

,併爲下一個查詢我想要的總和(這些ID的數量)。 但我不知道怎麼寫這個查詢,這裏是我的示例:

$query = mysql_query("SELECT sum(qty) FROM prereservation where id = $sample"); 

     while($rows = mysql_fetch_array($query)) 
      { 
      $total_qty = $rows['sum(qty)']; 
      }  

這樣的總和(數量)等於6 請糾正我的編碼錯誤,謝謝你們...

最後我找到了解決辦法,這是...

$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

[**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護,[棄用過程](http://j.mp/Rj2iVR)已經開始。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

回答

1

雖然沒有在子查詢中使用UNION ALL來組合三個查詢,並計算它們的總和qty

SELECT SUM(qty) 
FROM tableName 
FROM 
    (
     SELECT * 
     FROM prereservation 
     where ('$arival' BETWEEN arrival and departure) and 
       room_id ='11' and 
       status = 'active' 
     UNION ALL 
     SELECT * 
     FROM prereservation 
     where ('$departure' BETWEEN arrival and departure) and 
       room_id ='11' and 
       status = 'active' 
     UNION ALL 
     SELECT * 
     FROM prereservation 
     where (arrival > '$arival' and departure < '$departure') and 
       room_id ='11' and 
       status = 'active' 
    ) a 
GROUP BY colName 
0

您可以在3個SQL語句中使用OR子句,因爲你基本上SELECT荷蘭國際集團*了同桌的3種不同情況下合二爲一。這樣你就可以確保擁有所有獨特的結果。

喜歡的東西:

SELECT * FROM prereservation where 
(
(arrival > '$arival' and departure < '$departure') 
or ('$departure' BETWEEN arrival and departure) 
or ('$arival' BETWEEN arrival and departure) 
) and room_id ='11' and status = 'active' 
0

您可以通過使用select ... union select ...所有三個查詢組合成一個查詢:

$query1 = mysql_query("SELECT * FROM prereservation where '$arival' BETWEEN arrival and departure and room_id ='11' and status = 'active' union SELECT * FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='11' and status = 'active' union SELECT * FROM prereservation where arrival > '$arival' and departure < '$departure' and room_id ='11' and status = 'active'"); 

那麼你不需要合併多個結果爲一體。

一種更簡單,更便宜的方式,雖然,是將where條款合併成一個,並使用:

select * from prereservation 
where ('$arival' between arrival and departure 
     or '$departure' between arrival and departure 
     or arrival > '$arival' and departure < '$departure') 
     and room_id ='11' and status = 'active' 
0

更好地結合起來的3個查詢到1

SELECT id, SUM(qty) as sumQty 
FROM prereservation 
WHERE 
(
    (arival BETWEEN '$arrival' AND 'departure') OR 
    (departure BETWEEN '$arrival' AND '$departure') OR 
    (arrival > '$arival' AND departure < '$departure') 
) 
    AND room_id ='11' 
    AND status = 'active' 
GROUP BY id 

的,你可以echo $rows3['id']. " - ". $rows3['sumQty'];

0

您應該在條件之間使用帶OR的單個查詢,並使用DISTINCT確保沒有重複項。至於最後一個查詢:

"SELECT sum(qty) FROM prereservation where id = $sample GROUP BY id;" 
相關問題