我有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>
溶液通過將所有的答案製成,謝謝你們..
[**請不要在新代碼**中使用'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)。 –