计数字段的总和以获得总和

发布于 2024-11-14 01:17:43 字数 870 浏览 3 评论 0原文

我对此很陌生,正在寻求一些帮助。我正在使用以下代码在当前系统中复制报告。

    <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝不服输 2024-11-21 01:17:43

正如所说,除非确实需要,否则永远不要在循环内使用查询。关于获取总和,如果您使用 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.

默嘫て 2024-11-21 01:17:43

我不确定这对性能的影响,但您可以尝试

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)

戏蝶舞 2024-11-21 01:17:43

我一开始误解了这个问题。这应该有效:

$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');";

I misunderstood the question at first. This should work:

$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');";
忆梦 2024-11-21 01:17:43
$countTotal = 0;
foreach() {
   $query = /*...*/
   $row = mysql_fetch_*($query);
   $countTotal += $row['count'];   
}

echo $countTotal;

无论如何,永远不要将查询放入循环中,而是使用 Join 。

$countTotal = 0;
foreach() {
   $query = /*...*/
   $row = mysql_fetch_*($query);
   $countTotal += $row['count'];   
}

echo $countTotal;

Anyway never put a query inside a loop, use Join instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文