我对此很新,我正在寻求帮助。我正在使用以下代码在当前系统中复制报告。
<?php
foreach ($possibleSanctionsAssociativeArray as $currentSanction => $currentSanctionDetailsArray)
{
$tableHeadersArray = array ('Home Office',' Total');
$query = "SELECT home_office, COUNT(file_id) FROM cases WHERE ".$currentSanction."='Yes' and ($refdate>='$begindate' AND $refdate<='$enddate') GROUP BY home_office";
$title = "<p class='report_title'> <b>".getSanctionDescriptiveName($currentSanction)."</b>";
simpleStatTable($query, $tableHeadersArray, $title);
}
?>
它显示一个包含两列和两行的表:
HOME OFFICE | Total
OJA | 82
ORL | 634
我希望它显示第三行“OVERALL TOTAL | 716”
我尝试了几个没有运气的解决方案,我想我正在努力计算一个计数字段的总和,然后显示计数。
提前感谢所有帮助。
答案 0 :(得分:5)
正如所说的那样,除非真的需要,否则永远不要在循环中使用查询。关于获得全部金额,如果您使用的是MySQL,可以使用WITH ROLLUP。
答案 1 :(得分:2)
我不确定这个的性能影响,但你可以尝试
SELECT SUM(totals) FROM (SELECT home_office, COUNT(file_id) AS totals FROM cases WHERE ".$currentSanction."='Yes' and ($refdate>='$begindate' AND $refdate<='$enddate') GROUP BY home_office AS counts_table)
答案 2 :(得分:1)
我一开始误解了这个问题。这应该有效:
$query = "SELECT home_office, COUNT(file_id) FROM cases
WHERE ".$currentSanction."='Yes' and ($refdate>='$begindate' AND $refdate<='$enddate')
GROUP BY home_office
UNION
SELECT 'Overall Total' AS home_office, COUNT(file_id) FROM cases
WHERE ".$currentSanction."='Yes' and ($refdate>='$begindate' AND $refdate<='$enddate');";
答案 3 :(得分:0)
$countTotal = 0;
foreach() {
$query = /*...*/
$row = mysql_fetch_*($query);
$countTotal += $row['count'];
}
echo $countTotal;
无论如何从不在循环中放置查询,而是使用Join。