ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
接着上一篇[https://www.cnblogs.com/yoyoketang/p/10065424.html](https://www.cnblogs.com/yoyoketang/p/10065424.html),继续学生表SQL * 1.计算每个人的平均成绩, 要求显示字段: 学号,姓名,平均成绩 * 2.计算每个人的成绩,总分数,平均分,要求显示:学号,姓名,语文,数学,英语,总分,平均分 * 3.列出各门课程的平均成绩,要求显示字段:课程,平均成绩 * 4.列出数学成绩的排名, 要求显示字段:学号,姓名,成绩,排名 <br /> ## **万年不变学生表** 有2张表,学生表(student)基本信息如下 ![](https://img.kancloud.cn/7b/dd/7bddde959bbe4616f75b522ca88d2ace_519x280.png) 科目和分数表(grade) ![](https://img.kancloud.cn/6e/7d/6e7d27edc175fd7abf3b34189226262b_521x340.png) <br /> ### **计算学生平均分数** 1.计算每个人的平均成绩, 要求显示字段: 学号,姓名,平均成绩 ``` select a.id, a.name, avg(b.score) from student a, grade b where a.id=b.id group by id ``` 或者 ~~~ select a.id, a.name, c.avg_score from student a, (select b.id, avg(b.score) as avg_score from grade b group by b.id )c where a.id = c.id ~~~ ![](https://img.kancloud.cn/41/f9/41f9dc458eb9c0216e04d258aaf3faf9_356x331.png) <br /> ### **统计各科目成绩** 2.计算每个人的成绩,总分数,平均分,要求显示:学号,姓名,语文,数学,英语,总分,平均分 使用case when 语法把科目字段分解成具体的科目:语文,数学, 英语 ~~~ select a.id as 学号, a.name as 姓名, (case when b.kemu='语文' then score else 0 end) as 语文, (case when b.kemu='数学' then score else 0 end) as 数学, (case when b.kemu='英语' then score else 0 end) as 英语 from student a, grade b where a.id = b.id ~~~ ![](https://img.kancloud.cn/4b/d2/4bd206c14d2becb342e7876e7dc1a52e_649x443.png) ~~~ SELECT a.id as 学号, a.name as 姓名, sum(case when b.kemu='语文' then score else 0 end) as 语文, sum(case when b.kemu='数学' then score else 0 end) as 数学, sum(case when b.kemu='英语' then score else 0 end) as 英语, sum(b.score) as 总分 , sum(b.score)/count(b.score) as 平均分 FROM student a, grade b where a.id = b.id GROUP BY b.id, b.id ~~~ ![](https://img.kancloud.cn/40/c0/40c04656a6178c20bc7a9519a01914d8_630x317.png) <br /> ### **每门课程平均成绩** 3.列出各门课程的平均成绩,要求显示字段:课程,平均成绩 ~~~ select b.kemu, avg(b.score) from grade b group by b.kemu ~~~ ![](https://img.kancloud.cn/7b/f3/7bf331e99ec97896ff8c1447df81fab7_530x207.png) <br /> ### **成绩排名** 4.列出数学成绩的排名, 要求显示字段:学号,姓名,成绩,排名 在查询结果表里面添加一个变量@paiming,让它自动加1 ~~~ SELECT t.id, t.score as 数学分数, @paiming := @paiming+1 as 排名 FROM (SELECT b.id, b.score FROM grade b WHERE b.kemu = '数学' ORDER BY score DESC) AS t, (SELECT @paiming := 0) r ~~~ ![](https://img.kancloud.cn/56/ee/56ee615757cc4d3f26e6a888eff133b9_647x318.png) 结合student表获取学生名称 ~~~ SELECT t.id, a.name,t.score as 数学分数, @paiming := @paiming+1 as 排名 FROM (SELECT b.id, b.score FROM grade b WHERE b.kemu = '数学' ORDER BY score DESC) AS t, (SELECT @paiming := 0) r, student a WHERE a.id = t.id ~~~ ![](https://img.kancloud.cn/b2/7c/b27c7cd1d33065880f6fb663e9a286cc_682x356.png) <br /> ### **同结果名次相同** 上图由于同一个分数的小伙伴,排名不一样,本着公平、公正、公开的原则,同一分数名次一样 ~~~ SELECT t.id, a.name,t.score as 数学分数, (CASE WHEN @temp = t.score THEN @paiming WHEN @temp := t.score THEN @paiming :=@paiming + 1 WHEN @temp = 0 THEN @paiming :=@paiming + 1 END) AS num FROM (SELECT b.id, b.score FROM grade b WHERE b.kemu = '数学' ORDER BY score DESC) AS t, (SELECT @paiming := 0, @temp := 0) r, student a WHERE a.id = t.id ~~~ ![](https://img.kancloud.cn/73/5c/735c6dbe46ba5c2ad475f9cbf91d062f_610x498.png) ### 排名相同的占个名次 ~~~ SELECT obj.id, obj.score as 数学, @rownum := @rownum + 1 AS num_tmp, @incrnum := (CASE WHEN @rowtotal = obj.score THEN @incrnum WHEN @rowtotal := obj.score THEN @rownum END) AS 排名 FROM (SELECT id, score FROM grade WHERE kemu = "数学" ORDER BY score DESC ) AS obj, (SELECT @rownum := 0 ,@rowtotal := NULL ,@incrnum := 0) r ~~~ ![](https://img.kancloud.cn/31/51/3151433cd9886543ca522e72de80178a_693x454.png)