目录
作业要求
基础配置
结果
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
2、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
3、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
4、查询"李"姓老师的数量
5、查询学过"张三"老师授课的同学的信息
创建名为mysql_exampleTest的库,并使用
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) )
插入学生表数据
insert into Student values('01' , 'zhaolei' , '1990-01-01' , 'man');
insert into Student values('02' , 'qiandian' , '1990-12-21' , 'man');
insert into Student values('03' , 'sunfeng' , '1990-05-20' , 'man');
insert into Student values('04' , 'liyun' , '1990-08-06' , 'man');
insert into Student values('05' , 'zhoumei' , '1991-12-01' , 'woman');
insert into Student values('06' , 'wulan' , '1992-03-01' , 'woman');
insert into Student values('07' , 'zengjv' , '1989-07-01' , 'woman');
insert into Student values('08' , 'wangjv' , '1990-01-20' , 'woman');
创建课程表
CREATE TABLE Course( c_id VARCHAR(20), c_name VARCHAR(20) NOT NULL DEFAULT '', t_id VARCHAR(20) NOT NULL, PRIMARY KEY(c_id) );
插入课程表数据
insert into Course values('01' , 'CHINESE' , '02');
insert into Course values('02' , 'MATH' , '01');
insert into Course values('03' , 'ENGLISH' , '03');
创建教师表
CREATE TABLE Teacher( t_id VARCHAR(20), t_name VARCHAR(20) NOT NULL DEFAULT '', PRIMARY KEY(t_id) );
插入教师表数据
insert into Teacher values('01' , 'zhangsan');
insert into Teacher values('02' , 'lisi');
insert into Teacher values('03' , 'wangwu');
创建成绩表
CREATE TABLE Score( s_id VARCHAR(20), c_id VARCHAR(20), s_Score INT(3), PRIMARY KEY(s_id,c_id) );
插入成绩表数据
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);
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
运行代码展示:
select st.*,sc.s_Score CHINESE' ,sc2.s_Score 'MATH' 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、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
运行代码展示:
select st.s_id,st.s_name,ROUND(AVG(sc.s_Score),2) 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;
运行结果展示:
3、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
运行代码展示:
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;
运行结果展示:
4、查询"李"姓老师的数量
运行代码展示:
select t.t_name,count(t.t_id) from Teacher t group by t.t_id having t.t_name like "li%";
运行结果展示:
5、查询学过"张三"老师授课的同学的信息
运行代码展示:
select st.* from Student st left join Score sc on sc.s_id=st.s_id left join Course c on c.c_id=sc.c_id left join Teacher t on t.t_id=c.t_id where t.t_name="zhangsan";
运行结果展示: