查询每个学生的各科成绩sql语句

2025年03月01日 13:55
有5个网友回答
网友(1):

1、查询每个学生的各科成绩sql语句:

select a.studentid,a.name,a.sex,v1.score as '语文',v2.score as '数学', v3.score as '英语',v4.score

as ‘哲学’, (v1.score+v2.score+v3.score+v4.score)/4 as ‘平均成绩’ from Stuednt a
left join

(select studentid,score from grade where cid=(select cid from course where cname='语文'))as v1

on a.studentid=v1.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='数学'))as v2

on a.studentid=v2.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='英语'))as v3

on a.studentid=v3.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='哲学'))as v4

on a.studentid=v4.studentid

order by a.studentid

2、sql数据库介绍:

(1)SQL是Structured Query Language(结构化查询语言)的缩写。SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言。在使用它时,只需要发出"做什么"的命令,"怎么做"是不用使用者考虑的。

(2)SQL功能强大、简单易学、使用方便,已经成为了数据库操作的基础,并且现在几乎所有的数据库均支持SQL。

(3)SQL数据库的数据体系结构基本上是三级结构,但使用术语与传统关系模型术语不同。

(4)在SQL中,关系模式(模式)称为"基本表"(base table);存储模式(内模式)称为"存储文件"(stored file);子模式(外模式)称为"视图"(view);元组称为"行"(row);属性称为"列"(column)。

网友(2):

mysql> select * from student;
+-----------+------+------+
| studentId | name | sex  |
+-----------+------+------+
|         1 | 王五 | 男   |
|         2 | 李明 | 女   |
+-----------+------+------+
2 rows in set (0.00 sec)

mysql> select * from course;
+-----+-------+
| cid | cname |
+-----+-------+
|   1 | 语文  |
|   2 | 数学  |
|   3 | 英语  |
|   4 | 哲学  |
+-----+-------+
4 rows in set (0.00 sec)

mysql> select * from grade;
+-----+-----------+------+-------+
| gid | studentId | cid  | score |
+-----+-----------+------+-------+
|   1 |         1 |    1 |    80 |
|   2 |         1 |    2 |    70 |
|   3 |         1 |    3 |    80 |
|   4 |         1 |    4 |    90 |
|   5 |         2 |    1 |    90 |
|   6 |         2 |    2 |    70 |
|   7 |         2 |    3 |    70 |
|   8 |         2 |    4 |    80 |
+-----+-----------+------+-------+
8 rows in set (0.00 sec)

mysql> select
    ->     grade.studentId as '序号',
    ->     name as '姓名',
    ->     sex as '性别',
    ->     sum(if(cname = '语文', score, 0)) as '语文',
    ->     sum(if(cname = '数学', score, 0)) as '数学',
    ->     sum(if(cname = '英语', score, 0)) as '英语',
    ->     sum(if(cname = '哲学', score, 0)) as '哲学',
    ->     avg(score) as '平均成绩'
    -> from
    ->     grade
    ->     left join
    ->     student
    ->     on grade.studentId = student.studentId
    ->     left join
    ->     course
    ->     on grade.cid = course.cid
    -> group by grade.studentId;
+------+------+------+------+------+------+------+----------+
| 序号 | 姓名 | 性别 | 语文 | 数学 | 英语 | 哲学 | 平均成绩 |
+------+------+------+------+------+------+------+----------+
|    1 | 王五 | 男   |   80 |   70 |   80 |   90 |  80.0000 |
|    2 | 李明 | 女   |   90 |   70 |   70 |   80 |  77.5000 |
+------+------+------+------+------+------+------+----------+
2 rows in set (0.00 sec)

mysql 的这样试试,其它数据库就不会了。

网友(3):

select 
row_number() over(order by name) as 序号
t.name as 姓名,
t.sex as 性别,
max(case when t.cname='语文' then score end) as 语文,
max(case when t.cname='数学' then score end) as 数学,
max(case when t.cname='英语' then score end) as 英语,
max(case when t.cname='哲学' then score end) as 哲学
from
(select a.name,a.sex,b.cname,c.score
from student a,course b,grade c 
where a.studentid=c.studentid
and b.cid=c.cid) as t
group by t.name,t.sex

网友(4):

select a.studentId,a.name,a.sex,c.cid,b.cname,c.score
into TableA
from Student a, Course b, Grade c
where a.studentId=c.studentId and c.cid=b.cid

select a.studentId,a.name,a.sex,
sum(case cname when "语文" then score else 0 end) as 语文,
sum(case cname when "数学" then score else 0 end) as 数学,
sum(case cname when "英语" then score else 0 end) as 英语,
sum(case cname when "哲学" then score else 0 end) as 哲学,
sum(score)*1.0/4 as "平均成绩"
from TableA
group by name

网友(5):

select a.studentid,a.name,a.sex,v1.score as '语文',v2.score as '数学', v3.score as '英语',v4.score
as ‘哲学’, (v1.score+v2.score+v3.score+v4.score)/4 as ‘平均成绩’ from Stuednt a
left join
(select studentid,score from grade where cid=(select cid from course where cname='语文'))as v1
on a.studentid=v1.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='数学'))as v2
on a.studentid=v2.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='英语'))as v3
on a.studentid=v3.studentid
left join
(select studentid,score from grade where cid=(select cid from course where cname='哲学'))as v4
on a.studentid=v4.studentid
order by a.studentid