8查询进阶

1.sum(case when c_id='01' then s_score else NULL end ) cid1,

加行记录为单列值,该新列和值

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)

-- 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩(难)

SELECT s_id,AVG(s_score) FROM score GROUP BY s_id ORDER BY AVG(s_score) DESC;#平均成绩从高到低显示所有学生的平均成绩

#sum作用加行记录

SELECT s_id,

sum(case when c_id='01' then s_score else NULL end ) cid1,

sum(case when c_id='02' then s_score else NULL end ) cid2,

sum(case when c_id='03' then s_score else NULL end ) cid3,

AVG(s_score) FROM score GROUP BY s_id ORDER BY AVG(s_score) DESC;

8查询进阶_第1张图片

#sum作用加行记录

select student.s_id,

sum(case when c_id='01' then s_score else NULL end ) cid1,

sum(case when c_id='02' then s_score else NULL end ) cid2,

sum(case when c_id='03' then s_score else NULL end ) cid3,

avg(s_score) average

from student,score

where student.s_id=score.s_id

group by student.s_id

order by average

你可能感兴趣的:(结构性学mysql,sql,数据库,java)