mysql统计-关于学生成绩
生活随笔
收集整理的這篇文章主要介紹了
mysql统计-关于学生成绩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注意前5個問題使用張三和李四兩個字段</span>
1.計算每個人的總成績并排名(要求顯示字段:姓名,總成績)
2.???計算每個人的總成績并排名(要求顯示字段: 學號,姓名,總成績)
SELECT stuid,`name`,SUM(score) FROM stuscore GROUP BY `name` ORDER BY SUM(score)
| stuid | name | SUM(score) |
| 1 | 張三 | 239 |
| 2 | 李四 | 240 |
3.???計算每個人單科的最高成績(要求顯示字段: 學號,姓名,課程,最高成績)
寫法1 SELECT stuid,`name`,`subject`,max(score) FROM stuscore group by stuid
寫法2: SELECT t1.stuid,t1.`name`,t1.`subject`,t1.score FROM stuscore t1, (SELECT `stuid`,MAX(score) AS maxscore FROM stuscore GROUP BY `stuid`) t2 where t1.stuid=t2.stuid and t1.score=t2.maxscore
| stuid | name | subject | score |
| 1 | 張三 | 數學 | 89 |
| 2 | 李四 | 數學 | 90 |
4.???計算每個人的平均成績(要求顯示字段: 學號,姓名,平均成績)
SELECT stuid,`name`,AVG(score) FROM stuscore group by stuid
| stuid | name | AVG(score) |
| 1 | 張三 | 79.6667 |
| 2 | 李四 | 80.0000 |
5.???列出各門課程成績最好的學生(要求顯示字段: 學號,姓名,科目,成績)
方法一: SELECT stuid,`name`,`subject`,max(score) FROM stuscore? GROUP BY `subject` 方法二: SELECT t1.stuid,t1.`name`,t1.`subject`,t2.maxscore FROM stuscore t1,(SELECT `subject`,MAX(score)as maxscore FROM stuscore GROUP BY `subject`)t2 WHERE t1.`subject`=t2.`subject` and t1.score=t2.maxscore
| stuid | name | subject | maxscore |
| 1 | 張三 | 語文 | 80 |
| 2 | 李四 | 數學 | 90 |
| 2 | 李四 | 英語 | 80 |
從問題6開始增加王五字段
6.???列出各門課程成績最好的兩位學生(要求顯示字段: 學號,姓名,成績)
select stuid,`name`,SUM(score)as sumscore from stuscore GROUP BY `name` order by sumscore DESC LIMIT 2
| stuid | name | sumscore |
| 3 | 王五 | 268 |
| 2 | 李四 | 240 |
7.列出數學成績的排名 select `name`,`subject`,score from stuscore where `subject`="數學" ORDER BY score DESC8. 求出李四的數學成績的排名
9.統計學科成績及格,良,優的個數
select subject, (select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格, (select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良, (select count(*) from stuscore where score >80 and subject=t1.subject) as 優 from stuscore t1? <span style="font-family: 宋體;">group by subject</span>
| subject | 及格 | 良 | 優 |
| 數學 | 0 | 0 | 3 |
| 英語 | 0 | 2 | 1 |
| 語文 | 0 | 2 | 1 |
</pre><pre name="code" class="sql">select stuid as 學號,name as 姓名, sum(case when subject='語文' then score else 0 end) as 語文, sum(case when subject='數學' then score else 0 end) as 數學, sum(case when subject='英語' then score else 0 end) as 英語, sum(score) as 總分,(sum(score)/count(*)) as 平均分 from stuscore group by stuid,name
總結
以上是生活随笔為你收集整理的mysql统计-关于学生成绩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python如何将各种时间转换成时间戳
- 下一篇: bean must be of type