Php 组,点的总和,组
我的桌子Php 组,点的总和,组,php,mysql,Php,Mysql,我的桌子 +----+-------+------+---------+--------+------+ | ID | CLASS | NAME | SCHOOL | POINTS | YEAR | +----+-------+------+---------+--------+------+ | 1 | 5 | S1 | School1 | 5 | 2013 | | 2 | 6 | S2 | School1 | 0 | 2013 | | 3
+----+-------+------+---------+--------+------+
| 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
}
?>
请帮帮我。分“两步”完成:
他希望学校的顺序是降序的,所以将DESC添加到学校的查询顺序中。谢谢VancleiP。学校总数正在显示,但5、6班的总和没有显示。哎呀,我忘了:改为包含$row2['Sum(Points)”,因为字段名是Sum(Points)很棒的代码。非常感谢你,万克里普。你的代码帮了我很多。再次感谢
| School1 | 14 |
--------------------
Class 5 6
Class 6 8
--------------------
| School2 | 3 |
Class 5 0
Class 6 3
+------+------+------+
<?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
}
?>