我可以使用单个SQL语句返回我可以通过PHP显示的三个单独结果吗?我希望在一个有三种不同条件的桌子上制定三种不同的“总数”。
例如..... ......
$resultGetTotals = SELECT COUNT(Total1) FROM table_xyz WHERE Status = X AS Total1
SELECT COUNT(Total2) FROM table_xyz WHERE Status = X AND Person = Y AS Total2
SELECT COUNT(Total3) FROM table_xyz WHERE Status = Y AND Person = X AS Total3
while ($rGetTotals = mysql_fetch_array($resultGetTotals)){
$Total1 = $rGetTotals["Total1"]
$Total2 = $rGetTotals["Total2"]
$Total2 = $rGetTotals["Total2"];
}
Total One is: <?php print $Total1; ?><br>
Total Two is: <?php print $Total2; ?><br>
Total Three is: <?php print $Total3; ?><br>
任何帮助都非常感谢:)
答案 0 :(得分:2)
SELECT SUM(CASE WHEN t.status = X THEN 1 ELSE 0 END) AS total1,
SUM(CASE WHEN t.status = X AND t.person = Y THEN 1 ELSE 0 END) AS total2,
SUM(CASE WHEN t.status = Y AND t.person = X THEN 1 ELSE 0 END) AS total3
FROM TABLE_XYZ t
$result = mysql_query("SELECT SUM(CASE WHEN t.status = X THEN 1 ELSE 0 END) AS total1,
SUM(CASE WHEN t.status = X AND t.person = Y THEN 1 ELSE 0 END) AS total2,
SUM(CASE WHEN t.status = Y AND t.person = X THEN 1 ELSE 0 END) AS total3
FROM TABLE_XYZ t");
while ($row = mysql_fetch_array($result)) {
echo "Total One is: $row['total1']<br>"
echo "Total Two is: $row['total2']<br>"
echo "Total Three is: $row['total3']<br>"
}
mysql_free_result($result);
答案 1 :(得分:0)
假设您想要每个用户的行数(而不是三个不同列中的非空值的数量),然后是所有用户,并进一步假设表中只有Person X和Y,使用方法:
SELECT Person, COUNT(*) FROM table_xyz GROUP BY Person
检索两个总计(对于人X和人Y)并将它们加在一起以获得总计。