我有一张包含一些列的表格(见下图):
我的表:
我想计算那些列中的值(多少" ok"" no"我有超过05,超过15等列)使用一些过滤器(即在1.20和1.50之间的best_bets)并使用php
显示它们现在我可以使用以下代码对列进行计数:
$result=mysql_query("SELECT count(over15) as total from risultati WHERE over15 = 'OK' AND best_bets >= 1.15 AND best_bets <= 1.50");
$data=mysql_fetch_assoc($result);
echo $data['total'];
但我想为更多列做这件事
此致
编辑:我试过这种方式,但它不起作用:$result=mysql_query("SELECT SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05,
SUM(CASE WHEN over15 = 'OK' THEN 1 ELSE 0 END) AS OK_15,
SUM(CASE WHEN over25 = 'OK' THEN 1 ELSE 0 END) AS OK_25,
SUM(CASE WHEN over35 = 'OK' THEN 1 ELSE 0 END) AS OK_35,
SUM(CASE WHEN over45 = 'OK' THEN 1 ELSE 0 END) AS OK_45
FROM risultati
WHERE best_bets BETWEEN 1.15 AND 1.5O");
$data=mysql_fetch_assoc($result);
echo $data['OK_15'];
echo $data['OK_25'];
echo $data['OK_35'];
echo $data['OK_45'];
edit2:我也尝试过另一种方式,但没有:
$result=mysql_query("SELECT SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05,
SUM(CASE WHEN over15 = 'OK' THEN 1 ELSE 0 END) AS OK_15,
SUM(CASE WHEN over25 = 'OK' THEN 1 ELSE 0 END) AS OK_25,
SUM(CASE WHEN over35 = 'OK' THEN 1 ELSE 0 END) AS OK_35,
SUM(CASE WHEN over45 = 'OK' THEN 1 ELSE 0 END) AS OK_45
FROM risultati
WHERE best_bets BETWEEN 1.15 AND 1.5O");
while ($row = mysql_fetch_assoc($result))
{
echo $row['OK_05'];
echo $row['OK_15'];
echo $row['OK_25'];
echo $row['OK_35'];
echo $row['OK_45'];
}
答案 0 :(得分:0)
使用条件聚合:
SELECT SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05,
SUM(CASE WHEN over15 = 'OK' THEN 1 ELSE 0 END) AS OK_15,
SUM(CASE WHEN over25 = 'OK' THEN 1 ELSE 0 END) AS OK_25,
SUM(CASE WHEN over35 = 'OK' THEN 1 ELSE 0 END) AS OK_35,
SUM(CASE WHEN over45 = 'OK' THEN 1 ELSE 0 END) AS OK_45
FROM risultati
WHERE best_bets BETWEEN 1.15 AND 1.5O -- or whatever restrictions you want
此方法允许您在单个查询中收集所有年龄列的统计信息,而将列检查为“确定”。限制一个查询中的单个列。