mysql查詢學生所有學科總成績排名


學生表student

 

 

班級表class

 

 

課程表subject

 

 

成績表score

 

 

查詢所有學生各科成績按照總成績降序排列。

1.獲取單門課程的學生成績

select sc.stu_id,sc.score from score sc where sc.subject_id=1001

 


2.獲取所有學生的總成績

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

 


3.一共有三門課程,分別獲取這三門課程的學生成績表然后和學生總成績表聯合按總成績進行降序排列得到一張新表

select a1.stu_id,a1.score 'Java編程',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 a3.stu_id = a2.stu_id left join
(select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id) b
on a3.stu_id = b.stu_id
order by b.sumscore desc

 

4.和學生表聯合查詢得到學生姓名

select stu.name,c.Java編程,c.應用統計學,c.數據庫,c.總成績 from
(
select a1.stu_id,a1.score 'Java編程',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 a3.stu_id = a2.stu_id left join
(select sc.stu_id,sum(sc.score) sumscore from score sc group by sc.stu_id) b
on a3.stu_id = b.stu_id order by b.sumscore desc
) c
left join student stu on stu.id = c.stu_id;

 


得到題目要求的結果。
---------------------
原文:https://blog.csdn.net/zqmy_/article/details/84929955 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM