计数字段的总和以获得总和
我对此很陌生,正在寻求一些帮助。我正在使用以下代码在当前系统中复制报告。
<?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
我希望它显示第三行,上面写着“总体总计 | 716”
我尝试了几种解决方案,但没有成功,我认为我正在努力解决计数字段的总和,然后显示计数。
感谢您提前提供的所有帮助。
I am very new to this and am looking for some help. I am replicating a report in my current system with the following code.
<?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);
}
?>
It displays a Table with two columns and two rows:
HOME OFFICE | Total
OJA | 82
ORL | 634
I would like it to display a third row that says "OVERALL TOTAL | 716"
I have tried several solutions with no luck, I think I am struggling with the sum of a count field, then displaying the count.
Thanks for all the help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如所说,除非确实需要,否则永远不要在循环内使用查询。关于获取总和,如果您使用 MySQL,您可以使用 带 ROLLUP。
As it was said, never use a query inside a loop unless it's really needed. About getting the whole sum, in case you're using MySQL, you could use the WITH ROLLUP.
我不确定这对性能的影响,但您可以尝试
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)
I'm unsure about the performance implications of this one, but you could try
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)
我一开始误解了这个问题。这应该有效:
I misunderstood the question at first. This should work:
无论如何,永远不要将查询放入循环中,而是使用 Join 。
Anyway never put a query inside a loop, use Join instead.