[英]PHP SQL - Multiple select from one table
I have a SELECT
query from one table that works great as follows: 我有一个来自一个表的
SELECT
查询,其工作原理如下:
$sql = "SELECT count(max720) AS anzahlpositive1 from table where (((max720 - lastsignal)/lastsignal)*100) >= 1 AND (((max720 - lastsignal)/lastsignal)*100) < 2 AND max720 != ''";
$result = $conn->query($sql);
But I had 20-30 of them with other values. 但是我有20-30个具有其他值。 For every
SELECT
query a new connection established.. I think thats a lot of perfomance. 对于每个
SELECT
查询,都会建立一个新的连接。.我认为这是很多性能。
Thats the 2nd: 那是第二个:
$sql = "SELECT count(max720) AS anzahlpositive2 from table where (((max720 - lastsignal)/lastsignal)*100) >= 2 AND (((max720 - lastsignal)/lastsignal)*100) < 3 AND max720 != ''";
$result = $conn->query($sql);
Is is possible to do this in ONE select? 可以一次选择吗? (better performance)
(更好的性能)
I have tried with UNION
(but i think its for 2 or more tables) 我已经尝试了
UNION
(但是我认为它适合2个或更多表)
Is that a solution? 这是解决方案吗?
$sql = "SELECT 1";
$sql .= "SELECT 2";
$sql .= "SELECT 3";
...
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $anzahlpositive1 ;
echo $anzahlpositive2 ;
echo $anzahlpositive3 ;
....
}
select count(max720) AS ...
union all
select count(max720) AS ...
You can use as many unions as you want, read the difference between union
and union all
, union
will discard duplicates. 您可以根据需要使用任意多个联合,读取
union
和union all
之间的区别, union
将丢弃重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.