日期:2014-05-16  浏览次数:20584 次

mysql max(sum())问题
原题目是学生成绩管理系统:查出成绩总和最高的学生的名称及总分数.(MYSQL)

成绩表:
  <hibernate-mapping auto-import="false">
<class name="org.hibernate.solution.studentcourse.Score" table="sc_score">
<id name= "scoreId">
<generator class="native"/>
</id>
<many-to-one name="student" column="studentid"/>
<many-to-one name="course" column="courseid"/>
<property name="scoreValue"  />
</class>
</hibernate-mapping>

学生表:
<hibernate-mapping auto-import="false">
<class name="org.hibernate.solution.studentcourse.Student" table="sc_student">
<id name="studentNo">
<generator class="native"/>
</id>
<property name="studentName" />
<property name="sex" />

</class>
</hibernate-mapping>


我尝试用以下语句查询:
select studentid,studentName,max(sumScoreValue) from
(
select sum(scoreValue) as sumScoreValue, a.studentid,b.studentName
from sc_score a , sc_student b
where a.studentid = b.studentNo  group by studentid
)table1

得到的结果 max(sumScoreValue)正确取得最高分数,但是studentid,studentName是默认第一个学生的数值。

无柰之下,我用以下语句进行查询:
select max(sumScoreValue) as sumScoreValue, studentid,studentName  from  (
select sum(scoreValue) as sumScoreValue, a.studentid,b.studentName
from sc_score a , sc_student b
where a.studentid = b.studentNo  group by studentid
)table1 where sumScoreValue = (
select max(scoreValue) from (
  select sum(scoreValue) as scoreValue from sc_score group by studentid
)table2
)

上面那语句肯定不行的,这问题说白了就是max(sum(scoreValue)),但是MySQL好象不支持。

哪位大侠可以帮忙写个SQL语句?