一、建表
1、建库、建表
# 创建数据库
create database mysql_exampleTest;
use mysql_exampleTest;
# 学生表
CREATE TABLE Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT '',
s_birth VARCHAR(20) NOT NULL DEFAULT '',
s_sex VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(s_id)
);
# 课程表
CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT '',
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
# 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id)
);
# 成绩表
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_Score INT(3),
PRIMARY KEY(s_id,c_id)
);
2、插入数据:
# 插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
Student表:
+------+--------+------------+-------+
| s_id | s_name | s_birth | s_sex |
+------+--------+------------+-------+
| 01 | 赵雷 | 1990-01-01 | 男 |
| 02 | 钱电 | 1990-12-21 | 男 |
| 03 | 孙风 | 1990-05-20 | 男 |
| 04 | 李云 | 1990-08-06 | 男 |
| 05 | 周梅 | 1991-12-01 | 女 |
| 06 | 吴兰 | 1992-03-01 | 女 |
| 07 | 郑竹 | 1989-07-01 | 女 |
| 08 | 王菊 | 1990-01-20 | 女 |
+------+--------+------------+-------+
# 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
Course表:
+------+--------+------+
| c_id | c_name | t_id |
+------+--------+------+
| 01 | 语文 | 02 |
| 02 | 数学 | 01 |
| 03 | 英语 | 03 |
+------+--------+------+
# 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
Teacher表:
+------+--------+
| t_id | t_name |
+------+--------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 王五 |
+------+--------+
# 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
Score表:
+------+------+---------+
| s_id | c_id | s_Score |
+------+------+---------+
| 01 | 01 | 80 |
| 01 | 02 | 90 |
| 01 | 03 | 99 |
| 02 | 01 | 70 |
| 02 | 02 | 60 |
| 02 | 03 | 80 |
| 03 | 01 | 80 |
| 03 | 02 | 80 |
| 03 | 03 | 80 |
| 04 | 01 | 50 |
| 04 | 02 | 30 |
| 04 | 03 | 20 |
| 05 | 01 | 76 |
| 05 | 02 | 87 |
| 06 | 01 | 31 |
| 06 | 03 | 34 |
| 07 | 02 | 89 |
| 07 | 03 | 98 |
+------+------+---------+
二、实战
# 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select st.*,sc.s_Score as '语文' ,sc2.s_Score '数学'
from Student st
left join Score sc on sc.s_id=st.s_id and sc.c_id='01'
left join Score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'
where sc.s_Score>sc2.s_Score;
# 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select st.*,sc.s_Score '语文',sc2.s_Score '数学'
from Student st
left join Score sc on sc.s_id=st.s_id and sc.c_id='01'
left join Score sc2 on sc2.s_id=st.s_id and sc2.c_id='02'
where sc.s_Score<sc2.s_Score;
# 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
# 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
select st.s_id,st.s_name,(case when ROUND(AVG(sc.s_Score),2) is null then 0 else ROUND(AVG(sc.s_Score)) end ) cjScore
from Student st
left join Score sc on sc.s_id=st.s_id
group by st.s_id having AVG(sc.s_Score)<60 or AVG(sc.s_Score) is NULL;
# 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select st.s_id,st.s_name,count(sc.c_id) '选课总数',(case when sum(sc.s_Score) is null or sum(sc.s_Score)='' then 0 else sum(sc.s_Score) end) '总成绩'
from Student st
left outer join Score sc on st.s_id=sc.s_id
group by st.s_id;