简体   繁体   English

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

[英]Need to add total to union query that already has count

My problem has me stumped and I don't know where to educate myself on this challenge. 我的问题使我感到难过,而且我不知道该在哪里接受这项挑战的教育。

I have a bunch of kid's ages I'm counting up from data in a family event registration db built in php and mysql. 我有很多孩子年龄,我是从用php和mysql建立的家庭事件注册数据库中的数据开始计数的。 The kids are registering themselves and declaring the tribe group and their age they belong to. 孩子们正在登记自己,并宣布他们所属的部落群体和年龄。 There are multiple events scheduled for registration at any given time. 在任何给定时间安排了多个事件来注册。 At any time, the event hosts need to see the age counts. 在任何时候,活动主持人都需要查看年龄计数。 The total count of kids in each column works in a mysql window but not in html. 每列中的孩子总数在mysql窗口中起作用,但在html中不起作用。 The mysql results so far look like this: 到目前为止,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

Here's the code: 这是代码:

<?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";
?>

I plan to shorten this up with a do/while loop once it works. 我计划在工作时使用do / while循环来缩短此时间。 I'd like to get this into html using a prepared>statement approach. 我想使用prepare> statement方法将其输入到html中。 But I don't think my binding names are correct. 但我认为绑定名称不正确。 Here's what I have: 这是我所拥有的:

<?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>

Any suggestions or direction would be great? 有什么建议或方向会很好吗?

Thanks 谢谢

Here's the final answer. 这是最终答案。 The labels needed to be changes from $Item to $Ages 标签需要从$ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM