[英]combining results of 3 queries in one result
i have 3 queries and i want all result be in only one result using array_merge and array_unique. 我有3个查询,我希望所有结果都只能使用array_merge和array_unique在一个结果中。
here's the queries: 这是查询:
$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
i dont know if this is right? 我不知道这是对的吗?
$sample = array_unique(array_merge($rows1['id'], $rows2['id'], $rows3['id']))
i want to use array_merge and array_unique for id's of each results so the final result for id's that will be left is 我想使用array_merge和array_unique作为每个结果的ID,所以ID的最终结果将是
01, 02, 03, 04, 05, 08.
and for the next query all i want is the sum(qty) of those id's. 对于下一个查询,我想要的只是这些id的总和。 but i don't know how to write a query for this, here's my sample: 但我不知道如何为此编写查询,这是我的示例:
$query = mysql_query("SELECT sum(qty) FROM prereservation where id = $sample");
while($rows = mysql_fetch_array($query))
{
$total_qty = $rows['sum(qty)'];
}
so the sum(qty) is equal to 6. please correct my mistakes for coding, thanks guys... 所以sum(qty)等于6。请更正我的编码错误,谢谢大家...
Finally i found the solution, here it is.. 终于我找到了解决方案,就在这里。
$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>
solution was made by combining all of your answers, thank you guys.. 解决方案是结合您所有的答案而做出的,谢谢。
while not combine the three queries using UNION ALL
in subquery and calculate their total qty
? 同时不在子查询中使用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
You can combine your 3 SQL statements into one using OR
clause because you are basically SELECT
ing *
over same table for 3 different cases. 您可以使用OR
子句将3条SQL语句合并为一条,因为基本上是在3种不同情况下在同一表上执行SELECT
*
。 That way you can be sure to have all unique results. 这样,您可以确保获得所有独特的结果。
Something like: 就像是:
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'
You can combine all three queries into one query by using select ... union select ...
: 您可以使用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'");
Then you don't need to merge multiple results into one. 然后,您无需将多个结果合并为一个。
An easier and less expensive way though, would be to merge the where
clauses into one and use that: 不过,一种更简单,更便宜的方法是将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'
Better combine your 3 queries to 1 最好将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
The you can echo $rows3['id']. " - ". $rows3['sumQty'];
您可以echo $rows3['id']. " - ". $rows3['sumQty'];
echo $rows3['id']. " - ". $rows3['sumQty'];
You should use a single query with OR between your conditions and use DISTINCT to make sure there are no duplicates. 您应该在条件之间使用带有OR的单个查询,并使用DISTINCT来确保没有重复项。 As for the last query: 至于最后一个查询:
"SELECT sum(qty) FROM prereservation where id = $sample GROUP BY id;"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.