查询每个学生的各科成绩sql语句
表的结构如下: Student( studentId, name, sex) 分别表示: 学号,姓名, 性别
Course ( cid , cname) 分别表示: 课程号,课程名
Grade ( gid, studentId , cid, score) 分别表示: 成绩编号, 学号, 课程号, 成绩
现在要查询每个学生的各科成绩和平均分
查询结果打印出的样式如下:
序号 姓名 性别 语文 数学 英语 哲学 平均成绩
1 王五 男 80 70 80 90 80
2 李明 女 90 70 70 80 77.5
列名 语文, 数学, 英语, 哲学 是 Course 中的课程名, 查询出的样式一定要与上面的一样,(假设 Course 表中 cname 只有四个 语文, 数学, 英语, 哲学)
在线等,如果答出来了,本人一定重赏 展开
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)。
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 的这样试试,其它数据库就不会了。
嗯,mysql 也是关系数据库,语法和 sqlserver 差不多,多谢谢你啊
:D
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
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
这个语句你执行了没,怎么执行不对?
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
这个确实能执行,执行的结果与样式的一模一样,不过sql语句太复杂了,能简单些吗?
需要简单的结果的过程有时候是很复杂的,其实这个语句还算简单的,只不过每个科目的成绩都是个视图,然后连接起来。
有些查询嵌套多层的子查询,那才复杂呢。