php - 需要将总计添加到已有计数的联合查询中

标签 php mysql count sum union

我的问题困扰着我,我不知道在哪里进行 self 教育来应对这一挑战。

我有很多 child 的年龄,我正在根据 php 和 mysql 内置的家庭事件注册数据库中的数据来计算。 children 正在登记自己并宣布他们所属的部落群体和年龄。在任何给定时间都有多个事件可供注册。事件主办方需要随时查看年龄统计。每列中的 child 总数在 mysql 窗口中有效,但在 html 中无效。到目前为止 mysql 结果如下所示:

custom  item_number1    Age 7   Age 8  ....
703198  Apache            1     NULL
703198  Arapahoe          2     5
703198  Aztec             1     NULL
703198  Blackfoot         1     5
703198  Cherokee          1     1
703198  Chippewa          1     1
703198  Creek             1     2
703198  Fox               1     1
703198  Iroquois          1     NULL
703198  Mohawk          NULL    4
703198  Pawnee            1     2
703198  Yellowknives      2     1
703198  All Tribes        13    22

代码如下:

<?php
$query = "SELECT 703198 AS custom, item_name1
     , COALESCE(item_number1,'All Tribes') AS item_number1
     , COUNT(CASE WHEN Ages = 4 THEN 4 ELSE NULL END) AS 'Age 4'
     , COUNT(CASE WHEN Ages = 5 THEN 5 ELSE NULL END) AS 'Age 5'
     , COUNT(CASE WHEN Ages = 6 THEN 6 ELSE NULL END) AS 'Age 6'
     , COUNT(CASE WHEN Ages = 7 THEN 7 ELSE NULL END) AS 'Age 7'
     , COUNT(CASE WHEN Ages = 8 THEN 8 ELSE NULL END) AS 'Age 8'
     , COUNT(CASE WHEN Ages = 9 THEN 9 ELSE NULL END) AS 'Age 9'
     , COUNT(CASE WHEN Ages = 10 THEN 10 ELSE NULL END) AS 'Age 10'
     , COUNT(CASE WHEN Ages = 11 THEN 11 ELSE NULL END) AS 'Age 11'
     , COUNT(CASE WHEN Ages = 12 THEN 12 ELSE NULL END) AS 'Age 12'
     , COUNT(CASE WHEN Ages = 13 THEN 13 ELSE NULL END) AS 'Age 13'
     , COUNT(CASE WHEN Ages = 14 THEN 14 ELSE NULL END) AS 'Age 14'
     , COUNT(CASE WHEN Ages = 15 THEN 15 ELSE NULL END) AS 'Age 15'
     , COUNT(CASE WHEN Ages = 16 THEN 16 ELSE NULL END) AS 'Age 16'
     , COUNT(CASE WHEN Ages = 17 THEN 17 ELSE NULL END) AS 'Age 17'
   FROM ( SELECT item_number1
              , item_name1
              , item_name3 AS Item
              , item_number3 AS Ages
           FROM tbl_pp_transactions 
          WHERE custom = 703198 
            AND item_name3 = 'Daughter'
            AND item_number3 IN (4,5,6,7,8,9,10,11,12,13,14,15,16,17)
         UNION ALL
         SELECT item_number1
              , item_name1
              , item_name4 AS Item
              , item_number4 AS Ages
           FROM tbl_pp_transactions 
          WHERE custom = 703198 
            AND item_name4 = 'Daughter'
            AND item_number4 IN (4,5,6,7,8,9,10,11,12,13,14,15,16,17)
         UNION ALL
         SELECT item_number1
              , item_name1
              , item_name5 AS Item
              , item_number5 AS Ages
           FROM tbl_pp_transactions 
          WHERE custom = 703198 
            AND item_name5 = 'Daughter'
            AND item_number5 IN (4,5,6,7,8,9,10,11,12,13,14,15,16,17)
         UNION ALL
         SELECT item_number1
              , item_name1
              , item_name6 AS Item
              , item_number6 AS Ages
           FROM tbl_pp_transactions 
          WHERE custom = 703198 
            AND item_name6 = 'Daughter'
            AND item_number6 IN (4,5,6,7,8,9,10,11,12,13,14,15,16,17)
       ) AS dt            
GROUP
    BY item_number1 WITH ROLLUP";
?>

一旦它起作用,我计划用 do/while 循环来缩短它。我想使用准备好的>语句方法将其放入html中。但我认为我的绑定(bind)名称不正确。这是我所拥有的:

<?php
if ($stmt = $con->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($custom, $item_name1, $item_number1, $Ages, $Item, $Item1, $Item3, $Item5, $Item7, $Item9, $Item11, $Item13, $Item15, $Item17, $Item19, $Item21, $Item23, $Item25);
?>
                    <table border="1" cellpadding="1">
                        <tr>
                            <th>Tribe</th>
                            <th>Age 4</th>
                            <th>Age 5</th>
                            <th>Age 6</th>
                            <th>Age 7</th>
                            <th>Age 8</th>
                            <th>Age 9</th>
                            <th>Age 10</th>
                            <th>Age 11</th>
                            <th>Age 12</th>
                            <th>Age 13</th>
                            <th>Age 14</th>
                            <th>Age 15</th>
                            <th>Age 16</th>
                            <th>Age 17</th>
                        </tr>
                        <?php
                            while ($stmt->fetch()) {
                                echo "<tr class='body_black'>";
                                printf("
                                <td><strong>%s</strong></td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                <td align='center'>%s</td>
                                ", $item_number1, $Ages, $Item, $Item1, $Item3, $Item5, $Item7, $Item9, $Item11, $Item13, $Item15, $Item17, $Item19, $Item21, $Item23, $Item25);
                                echo "</tr>";

                            }
                            $stmt->close();
                        }
                        ?>
                    </table>

有什么建议或方向会很好吗?

谢谢

最佳答案

这是最终答案。标签需要从 $Item 更改为 $Ages

$stmt->bind_result($custom, $item_name1, $item_number1, $Ages, $Ages1, $Ages3, $Ages5, $Ages7, $Ages9, $Ages10, $Ages13, $Ages15, $Ages17, $Ages19, $Ages21, $Ages23, $Ages25);

", $item_number1, $Ages, $Ages1, $Ages3, $Ages5, $Ages7, $Ages9, $Ages10, $Ages13, $Ages15, $Ages17, $Ages19, $Ages21, $Ages23, $Ages25);

关于php - 需要将总计添加到已有计数的联合查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25517266/

相关文章:

javascript - 使用 Ajax 提取特定元素

php - 在 PHP 中检查动态模板时出错

mysql - 将 6 个项目加在一起得出总数,但不要将它们的值加在一起

python - 计算 python 中的特定字母组而不排除它们

mysql - 如何在 MYSQL 中使用 COUNT 和 DISTINCT 为每个不同值创建单独的计数?

php - 维基百科上的 "edit section"功能是如何工作的?

python - 如何使用 python 在 MySQL db 中插入/检索存储为 BLOB 的文件

Java:Callablestatement.prepareCall 在 SELECT 中变成 CALL

mysql - 替换成查询语法

php - 将换行符转换为 <li> 标签