参考链接
需要数据库表:(使用phpmyadmin创建)
添加学生数据:1.学生表
创建学生表:
create table Student(SID varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
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' , '女');
课程表:CID --课程编号,Cname 课程名称,TID 教师编号
create table Course(CID varchar(10),Cname varchar(10),TID varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
create table Teacher(TID varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
4.成绩表:SID 学生编号,CID 课程编号,score 分数
create table 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"课程成绩高的学生的信息及课程分数:(一个学生的第一门课程和第二门课程同时存在)
1.1、查询同时存在"01"课程和"02"课程的情况
select a.* , b.score 课程01的分数,c.score 课程02的分数 from Student a , SC b , SC c where a.SID = b.SID and a.SID = c.SID and b.CID = '01' and c.CID = '02' and b.score > c.score
1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)
select a.* , b.score 课程01的分数,c.score 课程02的分数 from Student a
left join SC b on a.SID = b.SID and b.CID = '01'
left join SC c on a.SID = c.SID and c.CID = '02'
where b.score > isnull(c.score)
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
2.1、查询同时存在"01"课程和"02"课程的情况 #b表示01课程 c表示02课程
select a.* , b.score 课程01的分数 ,c.score 课程02的分数 from Student a , SC b , SC c
where a.SID = b.SID and a.SID = c.SID and b.CID = '01' and c.CID = '02' and b.score < c.score
2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
select a.* , b.score 课程01的分数 ,c.score 课程02的分数 from Student a
left join SC b on a.SID = b.SID and b.CID = '01'
left join SC c on a.SID = c.SID and c.CID = '02'
where IFNULL(b.score,0) < c.score
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT a.SID ,a.Sname,cast(avg(b.score) as decimal(18,2))avg_score
#学生编号 学生姓名 SELECT 列名 FROM 表名 AS 别名;
from student a, sc b
# a 学生表 sc 成绩表
WHERE a.SID=b.SID
#a b ID满足相等
group by a.SID,a.Sname
having cast(avg(b.score) as decimal(18,2))>=60
order by a.SID
CAST函数进行数据类型转换
HAVING 子句可以让我们筛选分组后的各组数据。
GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组—对这一列相同的进行求和
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
4.1、查询在sc表存在成绩的学生信息的SQL语句
SELECT a.SID,a.Sname,cast(AVG(b.score) as decimal(18,2)) avg_score
from Student a,sc b
where a.SID=b.SID
GROUP BY a.SID,a.Sname
having cast(avg(b.score)as decimal(18,2))<60
order by a.SID