mysql max(sum())问题

本文探讨了如何使用MySQL查询成绩总和最高的学生姓名及分数。通过分析成绩表与学生表的关系,尝试解决max(sum(scoreValue))的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题目是学生成绩管理系统:查出成绩总和最高的学生的名称及总分数.(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语句?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值