显示学生各科成绩和总成绩-面试被问到
表結構如下:
name ? ?schedule ? score
'張三' ? ? '語文' ? ? ? ? '20'
'張三' ? ? '數學' ? ? ? ? '60'
'張三' ? ? '英語' ? ? ? ? '80'
'李四' ? ? '數學' ? ? ? ? '30'
'李四' ? ? '英語' ? ? ? ? '60'
'李四' ? ? '語文' ? ? ? ? '80'
?
想要的結果如下:
姓名 ? ?語文 ? 數學 ? 英語 ? 總成績
'張三', ?'20', ?'60', ?'80', ?'160'
'李四', ?'80', ?'30', ?'60', ?'170'
需要的sql 語句如下:
select t.name,
sum((case when t.schedule ='語文' then t.score ELSE 0 END)) AS 語文,
sum((case when t.schedule ='數學' then t.score ELSE 0 END)) AS 數學,
sum((case when t.schedule ='英語' then t.score ELSE 0 END)) AS 英語,
sum(t.score) as 總成績
from account t
group by t.name;
?
?
另外想要獲取 每門課的最高成績的學生信息sql語句的寫法:
select b.* from (select max(score) score,`subject` from classes group by subject) a,classes b
where a.score = b.score and a.subject = b.subject ;
轉載于:https://www.cnblogs.com/baizhanshi/p/5287246.html
總結
以上是生活随笔為你收集整理的显示学生各科成绩和总成绩-面试被问到的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 据库中事务、会话、线程这几个概念是什么关
- 下一篇: 【Cocos2d-x for WP8 学