drzablspw01655860 2013-10-14 15:40
浏览 10
已采纳

集团,积分,集团

My table

+----+-------+------+---------+--------+------+
| ID | CLASS | NAME | SCHOOL  | POINTS | YEAR |
+----+-------+------+---------+--------+------+
|  1 |     5 | S1   | School1 |      5 | 2013 |
|  2 |     6 | S2   | School1 |      0 | 2013 |
|  3 |     5 | S3   | School2 |      1 | 2014 |
|  4 |     6 | S4   | School1 |      3 | 2013 |
|  5 |     6 | S5   | School2 |      1 | 2014 |
|  6 |     5 | S6   | School1 |      0 | 2013 |
|  7 |     6 | S7   | School2 |      3 | 2013 |
|  8 |     6 | S8   | School1 |      5 | 2013 |
|  9 |     5 | S9   | School1 |      1 | 2014 |
| 10 |     5 | S10  | School1 |      0 | 2013 |
| 11 |     6 | S11  | School2 |      5 | 2014 |
| 12 |     5 | S12  | School1 |      1 | 2013 |
+----+-------+------+---------+--------+------+

Here I would like to find Total points of each School with respect of Class and Year ordering by highest points.

Here is my Problem, I would like to display, Class 5, 6 points also within the Total points.

<?php
$sql="SELECT Class, School, SUM(Points) FROM students WHERE Year='2013'
GROUP BY Class,School ORDER BY SUM(Points)";

$result=mysql_query($sql);
$count=1;
while ($row = mysql_fetch_array($result)) 
{   
?>
    <table>
     <tr>
        <td><?php echo  $row['School'];?></td>
        <td><?php echo  $row["SUM(Points)"];?></td>
     </tr>

      <tr>
         <td>Class <?php echo  $row['Class'];?></td>
         <td><?php echo $row['Points'];?></td>
      </tr>
     </table>
    <?php
      }
    ?>

So my Final output looks:

| School1 |    14 |
--------------------
  Class 5       6
  Class 6       8
--------------------

| School2 |     3 |
  Class 5       0
  Class 6       3
+------+------+------+

Please help me.

  • 写回答

1条回答 默认 最新

  • douzi1991 2013-10-14 15:47
    关注

    Do it in "two steps":

    <?php
    $sql="SELECT School, SUM(Points) FROM students WHERE Year='2013'
    GROUP BY School ORDER BY SUM(Points)";
    
    $result=mysql_query($sql);
    $count=1;
    while ($row = mysql_fetch_array($result)) 
    {   
    ?>
        <table>
         <tr>
            <td><?php echo  $row['School'];?></td>
            <td><?php echo  $row["SUM(Points)"];?></td>
         </tr>
    
        <?php
    
          $sql2="SELECT Class, SUM(Points) FROM students WHERE Year='2013' and School = '" . $row['School'] . "'
          GROUP BY Class ORDER BY SUM(Points)";
          $result2=mysql_query($sql2);
          while ($row2 = mysql_fetch_array($result2)) 
          {  
    
          ?>
                <tr>
                   <td>Class <?php echo  $row2['Class'];?></td>
                   <td><?php echo $row2['SUM(Points)'];?></td>
                </tr>
          <?php
          }
          ?>
    
    
         </table>
        <?php
          }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行