如何计算每个组的总数按mysql设置分类

时间:2014-08-11 18:34:32

标签: php mysql sql

我可能没有使用问题的正确主题。但这是细节。我有3个数据表,其中2个是设置名称组名称。其余的是数据 - 用户数据库。这是db。

set_name

+--------+-----------+
| set_id | set_title |
+--------+-----------+
|   1    |   Set A   |
+--------+-----------+
|   2    |   Set B   |
+--------+-----------+

group_db

+--------+-----------+--------+
| grp_id | grp_title | set_id |
+--------+-----------+--------+
|   1    |  Grp. A   |    1   |
+--------+-----------+--------+
|   2    |  Grp. B   |    1   |
+--------+-----------+--------+
|   3    |  Grp. C   |    1   |
+--------+-----------+--------+
|   4    |  Grp. D   |    1   |
+--------+-----------+--------+
|   5    |  Grp. E   |    1   |
+--------+-----------+--------+
|   6    |  Grp. F   |    2   |
+--------+-----------+--------+

USER_DB

+--------+-----------+
| usr_id |   grp_id  |
+--------+-----------+
|   1    |     1     |
+--------+-----------+
|   2    |     1     |
+--------+-----------+
|   3    |     2     |
+--------+-----------+
|   4    |     1     |
+--------+-----------+
|   5    |     3     |
+--------+-----------+
|   6    |     4     |
+--------+-----------+
|   7    |     5     |
+--------+-----------+
|   8    |     5     |
+--------+-----------+
|   9    |     5     |
+--------+-----------+
|   10   |     6     |
+--------+-----------+

根据上面提供的信息。我期望一个汇总表,其中统计所有用户并按组分类并设置。例如:

+-----+--------------------------------------------+--------+
| SET |                    Set A.                  | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM |    3   |    1   |    1   |    1   |    3   |    1   |
+-----+--------------------------------------------+--------+
|TOTAL|                      9                     |    1   |
+-----+--------------------------------------------+--------+

这就是我的方式。

<table>
  <tr>
  <?
    $sql_set=mysqli_query($con,"SELECT *,count(group_db.grp_id) AS nGrp\n" 
                             . "FROM set_name\n"
                             . "INNER JOIN group_db ON set_name.set_id=group_db.set_id\n" 
                             . "GROUP BY set_name.set_id\n"
                             . "ORDER BY set_name.set_id asc");
    echo "<td>SET</td>";
    while($rec_set=mysqli_fetch_array($sql_set)){
      echo "<td colspan=\"$rec_set[nGrp]\">$rec_set[set_title]</td>";
    }
  ?>
  </tr>
  <tr>
  <?
    $sql_sGrp=mysqli_query($con,"SELECT * from group_db\n" 
                              . "WHERE set_id='$rec_set[set_id]'\n" 
                              . "ORDER BY grp_title asc");
    echo "<td>GROUP</td>";
    while($rec_sGrp=mysqli_fetch_array($sql_sGrp)){
      echo "<td>$rec_sGrp[grp_title]</td>";
    }
  ?>
  </tr>
</table>

就是这样。我不知道该怎么走。请咨询。 PS。我应该将它们全部放在多级数组中以使其更容易吗?

2 个答案:

答案 0 :(得分:1)

我会做类似的事情:

SELECT 
    *
FROM
    user_db u
        JOIN
    group_db g ON u.grp_id = g.grp_id
        JOIN
    set_name s ON g.set_id = s.set_id

(编辑:将qry改为此^,可在此处查看:http://sqlfiddle.com/#!2/e749f/4

然后在PHP中:

$newArray = array();

while($rec_set=mysqli_fetch_array($sql_set)){
    $newArray[$rec_set['set_title']][$rec_set['grp_title']] += 1;
}

它应该为您提供一个很好的多维数组结果,您可以根据需要进行解析

并提供一个看起来像的表:

+-----+--------------------------------------------+--------+
| SET |                    Set A.                  | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM |    3   |    1   |    1   |    1   |    3   |    1   |
+-----+--------------------------------------------+--------+
|TOTAL|                      9                     |    1   |
+-----+--------------------------------------------+--------+

我会用:

<tr>
    <td>SET</td>
    <?php foreach($newArray as $set => $group): ?>
    <td colspan="<?=count($newArray[$set])?>"><?=$set?></td>
    <?php endforeach; ?>
</tr>
<tr>
    <td>GROUP</td>
    <?php foreach($newArray as $set => $group): ?>
    <?php foreach($group as $group_name => $amount): ?>
    <td><?=$group_name?></td>
    <?php endforeach; ?>
    <?php endforeach; ?>
</tr>
<tr>
    <td>NUMBER</td>
    <?php foreach($newArray as $set => $group): ?>
    <?php foreach($group as $group_name => $amount): ?>
    <td><?=$amount?></td>
    <?php $totals[$set] += $amount;?>
    <?php endforeach; ?>
    <?php endforeach; ?>
</tr>
<tr>
    <td>TOTAL</td>
    <?php foreach($newArray as $set => $group): ?>
    <td colspan="<?=count($newArray[$set])?>"><?=$totals[$set]?></td>
    <?php endforeach; ?>
</tr>

但是,现在我看看你将如何实际显示它,如果你真的想要一个看似你放置的表,那么多维数组可能不是循环数据的最佳方式,因为所有这些循环都是丑陋! (并且当您添加越来越多的集合和组时,它不会在水平方向上扩展得太好)。我没有检查它的准确性。

答案 1 :(得分:1)

echo '<table>';

$rows = array('SET', 'GROUP', 'NUM', 'TOTAL');
$setids = array();
$grp_usercounts = array();
$set_usertotals = array();

foreach($rows as $key => $row){
    echo "<tr> $rows </td>";
    switch ($key){
        case 0: //SET
            $sql = "SELECT s.set_id, set_title, count(g.grp_id) nGrp 
                FROM set_name s 
                JOIN group_db g ON s.set_id = g.set_id   
                group by set_id";
             $sql_set = mysqli_query($con, $sql);
             while($rec_set=mysqli_fetch_array($sql_set)){
                echo '<td colspan="'.$rec_set['nGrp'].'">'. rec_set['set_title'].'</td>';
                $setids[$rec_set['set_id']] = $rec_set['nGrp'];
            }
            break;

           case 1://GROUP

           foreach($setids as $setid => $val){

            $sql = "SELECT g.grp_id, grp_title, count(usr_id) nUsr
                FROM group_db g
                JOIN user_db u ON u.grp_id = g.grp_id
                where set_id = $setid 
                group by g.grp_id order by grp_title";
            $sql_set = mysqli_query($con, $sql);
            $total = 0;
            while($rec_set=mysqli_fetch_array($sql_set)){
                echo '<td>'. $rec_set['grp_title'].'</td>';
                $grp_usercounts[$rec_set['grp_id']] = $rec_set['nUsr'];
                $total += $rec_set['nUsr'];
            }   
            $set_usertotals[$setid] = $total;
        }
        break;

    case 2://NUM

        foreach($grp_usercounts as $key => $grp_usercount){
            echo '<td>'. $grp_usercount .'</td>';
        }
        break;

    case 3: //TOTAL

        foreach($set_usertotals as $setid => $set_usertotal){
            echo '<td colspan="'.$setids[$setid].'">'. $set_usertotal .'</td>';
        }
        break;

    }

}

unset($setids);
unset($grp_usercounts);
unset($set_usertotals);

echo '</table>';