需要添加总计已经计数的联合查询

时间:2014-08-27 00:44:31

标签: php mysql count sum union

我的问题让我感到难过,而且我不知道在哪里教育自己应对这一挑战。

我有一群孩子的年龄,我从用php和mysql构建的家庭事件注册数据库中的数据算起来。孩子们正在报名,宣布部落群体和他们所属的年龄。在任何给定时间都有多个活动计划进行注册。在任何时候,活动主持人都需要查看年龄计数。每列中的孩子总数在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循环工作后将其缩短。我想使用准备好的&gt;语句方法将其转换为html。但我不认为我的绑定名称是正确的。这就是我所拥有的:

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

任何建议或方向都会很棒?

由于

1 个答案:

答案 0 :(得分:0)

这是最后的答案。标签需要从$ 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);