集团,积分,集团

时间:2013-10-14 15:40:56

标签: php mysql

我的表

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

在这里,我希望找到每个学校的最高分的课程和年级排序总分。

这是我的问题,我想在总分数内显示5级,6分。

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

所以我的最终输出看起来是:

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

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

请帮帮我。

1 个答案:

答案 0 :(得分:1)

以“两步”完成:

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