mysql查询每个学生最高分_mysql查询各班最高分学生的信息

学生表student

d3b69774dc7c5f602918612d27575ecc.png

班级表class

dfd8c2e64c0e290da521b1596920d17a.png

课程表subject

683d438f3aa44c9527b02afcd00348aa.png

成绩表score

7328356bb1d8caf0dd97a09d0a7ae844.png

一、查询各班最高分学生的信息

1.从成绩表score中查询每个学生的总成绩并按降序排列

select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id order by sumscore desc

11c8ec1a61dc57a1ac9b10271d9537c9.png

2. 用1得到的表和学生表student联合查询得到学生个人信息

select stu.class_id,stu.name,stu.sex,a.sumscore from student stu

inner join

(select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id order by sumscore desc) a

on stu.id = a.stu_id

bb0da319d353ed697567d0bac28be009.png

3.用group by按class_id字段进行分组

select stu.class_id,stu.name,stu.sex,a.sumscore from student stu

inner join

(select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id order by sumscore desc) a

on stu.id = a.stu_id group by stu.class_id

b1b9ed1b4665928ab0fe98d20b36a4e2.png

4.和班级表class联合查询得到结果

select aa.name,aa.sex,cla.classname,aa.sumscore from

(select stu.class_id,stu.name,stu.sex,a.sumscore from student stu

inner join

(select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id order by sumscore desc) a

on stu.id = a.stu_id group by stu.class_id) aa

inner join class cla on cla.classid = aa.class_id;

55debc17e3b241c6689b9e7f0c53fdc7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值