gpt4 book ai didi

php - 从不同的列中选择多个值

转载 作者:行者123 更新时间:2023-11-29 04:24:16 26 4
gpt4 key购买 nike

我有 5 个不同的列,每列有 100 多行,每列都有各种数字。

我已经能够计算第 1 列中等于 3 的所有值

我希望能够计算所有 5 个不同列中的所有数字 3 并将它们相加

$countquery = "SELECT COUNT(*) FROM PowerBall WHERE W1=3";
$countresult = mysql_query($countquery) or die(mysql_error());

while($countrow = mysql_fetch_array($countresult)) {
echo "<br />";
echo "There are ". $countrow['COUNT(*)']."-3s";
}

最佳答案

您可以使用带有 CASE 表达式的聚合函数来获取所有列中 3 个值的总数。

Select
sum(case when col1 = 3 then 1 else 0 end) TotalCol1,
sum(case when col2 = 3 then 1 else 0 end) TotalCol2,
sum(case when col3 = 3 then 1 else 0 end) TotalCol3,
sum(case when col4 = 3 then 1 else 0 end) TotalCol4,
sum(case when col5 = 3 then 1 else 0 end) TotalCol5
from PowerBall

如果你想把它放在一栏中,那么你可以使用:

select TotalCol1 + TotalCol2 + TotalCol3 + TotalCol4 + TotalCol5
from
(
Select
sum(case when col1 = 3 then 1 else 0 end) TotalCol1,
sum(case when col2 = 3 then 1 else 0 end) TotalCol2,
sum(case when col3 = 3 then 1 else 0 end) TotalCol3,
sum(case when col4 = 3 then 1 else 0 end) TotalCol4,
sum(case when col5 = 3 then 1 else 0 end) TotalCol5
from PowerBall
) src

甚至:

select sum(Total)
from
(
Select count(col1) Total
from PowerBall
where col1 = 3
union all
Select count(col2)
from PowerBall
where col2 = 3
union all
Select count(col3)
from PowerBall
where col3 = 3
union all
Select count(col4)
from PowerBall
where col4 = 3
union all
Select count(col5)
from PowerBall
where col5 = 3
) src

关于php - 从不同的列中选择多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15034057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com