mysql查詢學生全部學科總成績排名

學生表student數據庫

 

 

班級表class編程

 

 

課程表subjectspa

 

 

成績表score.net

 

 

查詢全部學生各科成績按照總成績降序排列。blog

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

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

 


2.獲取全部學生的總成績im

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 

相關文章
相關標籤/搜索