mysql 统计学生成绩-面试题

先创建三个表 学生表、成绩表、科目表

1、查询单个科目成绩   结果如下图

select stu_id, score from score where subject_id = 1001;

 

2、查询学生总成绩   结果如下图

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

3、查询学生各科成绩和总成绩,在一张表上显示出来,总成绩高的在前

select a1.stu_id, a1.score '数学', a2.score '英语', a3.score '语文', b.sumscore '总成绩' from (select sc.stu_id,sc.score from score sc 
where sc.subject_id=1001) a1
left join (select sc.stu_id, sc.score from score sc where sc.subject_id=1002) 
a2 on a1.stu_id = a2.stu_id
left join (select sc.stu_id, sc.score from score sc where sc.subject_id=1003)
a3 on a1.stu_id = a3.stu_id
left join (select stu_id, sum(score) sumscore from score group by stu_id) 
b on b.stu_id = a1.stu_id order by b.sumscore desc

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值