我有3个查询,我希望所有结果只能在一个结果中使用array_merge和array_unique。
这是查询:
$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的总和(qty)。 但我不知道如何为此写一个查询,这是我的样本:
$query = mysql_query("SELECT sum(qty) FROM prereservation where id = $sample");
while($rows = mysql_fetch_array($query))
{
$total_qty = $rows['sum(qty)'];
}
所以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 :(得分: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
答案 1 :(得分:0)
您可以使用OR
子句将3个SQL语句合并为一个,因为对于3种不同的情况,您基本上SELECT
*
在同一个表上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'
。这样你就可以确保获得所有独特的结果。
类似的东西:
{{1}}
答案 2 :(得分: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'
答案 3 :(得分: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'];
答案 4 :(得分:0)
您应该在条件之间使用带有OR的单个查询,并使用DISTINCT确保没有重复项。至于最后一个查询:
"SELECT sum(qty) FROM prereservation where id = $sample GROUP BY id;"