做了些修改,水平一般,正在学习中,如有错误请指正。
CREATE DATABASE IF NOT EXISTS school DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use school;
# 学生表 Student
create table if not exists Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
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('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');
# 科目表 Course
create table if not exists Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
# 教师表 Teacher
create table if not exists Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
# 成绩表 SC
create table if not exists SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
# 1. 查询" 01 "课程比" 02 "课程成绩高的学生的ID及课程分数
select t1.SId, t1.score as 01_score, t2.score as 02_score, t3.Sname, t3.Sage, t3.Ssex from
(select SId, score from sc where CId='01') as t1 join
(select SId, score from sc where CId='02') as t2 join
Student t3
where t1.score>t2.score and t1.SId=t2.SId and t1.SId=t3.SId;
# 1.1 查询同时存在学习"01"课程和"02"课程的学生ID
select t1.SId from
(select SId,CId from SC where CId = "01") as t1 join
(select SId,CId from SC where CId = "02") as t2
where t1.SId=t2.SId;
# 1.2 查询学习"01"课程但可能不学习"02"课程的学生ID和课程ID(不学习的课程ID显示为 null)
select t1.SId, t1.CId, t2.CId from
(select SId,CId from SC where CId = "01") as t1 left join
(select SId,CId from SC where CId = "02") as t2
on t1.SId=t2.SId;
# 1.3 查询不学习" 01 "课程但学习" 02 "课程的学生ID和课程ID(不学习的课程ID显示为 null)
select t2.SId, t1.CId, t2.CId from
(select SId,CId from SC where CId = "01") as t1 right join
(select SId,CId from SC where CId = "02") as t2
on t1.SId=t2.SId;
# 2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select t1.SId, t2.Sname, convert(t1.score_sum/t1.course_num, decimal(10, 1)) as score_avg from
(select SId, sum(score) as score_sum, count(SId) as course_num from sc group by SId) as t1 join
student t2
where t1.SId=t2.SId and t1.score_sum/t1.course_num>60;
# 3. 查询在 SC 表存在成绩的学生信息
select t2.* from
(select distinct(SId) from sc) as t1 left join
student as t2
on t1.SId=t2.SId;
# 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null)
select t1.SId, t1.Sname, t2.course_num, t2.score_sum from
student as t1 left join
(select SId, sum(score) as score_sum, count(SId) as course_num from sc group by SId) as t2
on t1.SId=t2.SId;
# 4.1 查有成绩的学生信息
select t2.* from
(select distinct(SId) from sc) as t1 left join
student as t2
on t1.SId=t2.SId;
# 5. 查询「李」姓老师的数量
select count(*) from teacher where Tname like "李%";
# 6. 查询学过「张三」老师授课的同学的信息
select distinct(t1.SId) from
sc as t1 join
course t2 join
(select TId from teacher where Tname="张三") as t3
where t1.CId=t2.CId and t3.TId=t2.TId;
# 7. 查询没有学全所有课程的同学的信息
select * from
student t1 left join
(select distinct(SId), count(CId) as course_num from sc group by SId) as t2
on t1.SId=t2.SId and t2.course_num<3;
# 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct(SId) from sc where CId in(
select CId from sc where SId="01") and SId!="01";
# 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select distinct student.* from
student join
(select count(t2.SId) as course_num, t2.SId from (select SId, CId from sc) as t2 left join
(select SId,CId from sc where SId="01") as t3
on t2.CId=t3.CId group by t2.SId) as t1
where student.SId!="01" and t1.SId=student.SId and t1.course_num=3;
# 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select *
from student
where student.SId not in
(
select student.SId
from student left join sc on student.SId=sc.SId
where EXISTS
(select *
from teacher ,course
where teacher.Tname="张三"
and teacher.TId=course.TId
and course.CId=sc.CId))