MySQL语句强化-更新中

痞子三分冷 提交于 2020-03-06 09:08:40

参考资料:
数据库MySQL经典面试题之SQL语句

--  创建表------------------------------------------------------------
-- 学生表
drop table student;
create table Student(
SID varchar(10),
Sname varchar(10),
Sage datetime,
Ssex varchar(10)
);
-- SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
desc Student;

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' , '女');

-- 课程表
create table Course(
CID varchar(10),
Cname varchar(10),
TID varchar(10)
);
-- CID 课程编号,Cname 课程名称,TID 教师编号
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

-- 教师表
drop table Teacher;
create table Teacher(
TID varchar(10),
Tname varchar(10)
);
-- TID 教师编号,Tname 教师姓名
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

-- 成绩表
create table SC(
SID varchar(10),
CID varchar(10),
score decimal(18,1)
);
-- SID 学生编号,CID 课程编号,score 分数
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"课程的情况
--   思路:首先找出数据来源,这里是学生表和成绩表
--         其次找出判断条件,要求学号一样,课程名为01和02
--         分数的判断需要使用两次成绩表,因为是对同一个列的比较
select a.*, b.score 课程01的分数,c.score 课程02的分数 
from student a,sc b,sc c 
where (a.SID=b.SID) and (b.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 > ifnull(c.score,0);

-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
-- 把上面的>改为<就行了

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select sc.SID,student.Sname,cast(avg(sc.score) as decimal(18,2)) avg_score from sc 
left join student on sc.SID=student.SID -- on 先判断条件然后得到查询结果
group by SID
having avg(sc.score)>=60 -- having 聚集函数之后
order by SID;

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  -- where 从查询结果里筛选
group by a.SID , a.Sname
having cast(avg(b.score) as decimal(18,2)) >= 60 -- having 聚集函数之后
order by a.SID

-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--    查询学生表,根据学号再查成绩表,按照学号分组计数,算总成绩。
select student.SID '学生编号',student.Sname '学生姓名',count(sc.CID) '选课总数',sum(sc.score) '课程总成绩' from student,sc 
where student.SID=sc.SID
group by student.SID
order by student.SID

-- 6、查询"李"姓老师的数量
--    查询老师表,模糊查询姓名为李开头,计数
--    模糊查询使用like
select count(*)  李姓老师的数量 from teacher
where teacher.Tname like '李%'


-- 7、查询学过"张三"老师授课的同学的信息
--    查询学生表,根据学号查成绩表,根据老师编号查老师表,根据老师姓名得出结果
select a.* from student a,sc b, course c,teacher d
where a.SID=b.SID and b.CID=c.CID and c.TID=d.TID and d.Tname='张三';

-- 8、查询没学过"张三"老师授课的同学的信息
--     在学生表中排除上面查得的结果
select a.* from student a
where a.SID not in 
(select a.SID from student a,sc b, course c,teacher d
where a.SID=b.SID and b.CID=c.CID and c.TID=d.TID and d.Tname='张三');

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
-- 方法1 where     以学号为基准,两次查询成绩表获得01和02
select a.* from student a, sc b,sc c
where a.SID=b.SID and b.SID=c.SID and b.CID='01' and c.CID='02';

-- 方法2 exists    先查询学过01,再在结果中查询存在02
select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '01' 
and exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02')  
-- select 1 用于优化,只要存在就行数据不重要

-- 方法3 union all   分别找出01和02的表,组合在一起,再找出出现两次SID的记录
select m.* from Student m where SID in
(select SID from (select distinct SID from SC where CID = '01' union all select distinct SID from SC where CID = '02') t 
group by SID having count(1) = 2 )


-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
--     not exists 先查出学过01的学生,再排除学过02的学生
select Student.* from Student , SC 
where Student.SID = SC.SID and SC.CID = '01' 
and not exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02') 
order by Student.SID


-- 11、查询没有学全所有课程的同学的信息

--   方法1
select * from student a where a.SID in 
(select SID from
(select sc.*,count(sc.CID) from sc group by sc.SID
having count(sc.CID)< (select count(CID) from Course)) b)

-- 方法2 
select Student.*
from Student , SC
where Student.SID = SC.SID
group by Student.SID 
having count(CID) < (select count(CID) from Course)


MySQL常用SQL(含复杂SQL查询)
数据库on,where,having区别

1.on和where

所有的查询都回产生一个中间临时报表,查询结果就是从返回临时报表中得到。
on和where的区别

  • on是在临时报表生产之前,先根据限制条件对数据库记录进行过滤,然后生产临时报表;
  • where是在临时报表生产之后,根据限制条件从临时报表中筛选结果。

总结:在左外连接中,on会返回左表中的所有记录;而where中,此时相当于inner join,只会返回满足条件的记录。
速度:因为on限制条件发生时间较早,产生的临时报表数据集要小,因此on的性能要优于where。

2. having和where

  • having是在聚集函数计算结果出来之后筛选结果,查询结果只返回符合条件的分组,having不能单独出现,只能出现在group by子句中
  • where是在计算之前筛选结果,如果聚集函数使用where,那么聚集函数只计算满足where子句限制条件的数据。

总结:where即可以和select等其他子句搭配使用,也可以和group by子句搭配使用,where的优先级要高于having。
速度:因为where在聚集函数之前筛选数据,having在计算之后筛选分组,因此where的性能要优于having。

下面两种实现等价:

-- 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
-- 方法1:
select sc.SID,student.Sname,cast(avg(sc.score) as decimal(18,2)) avg_score from sc 
left join student on sc.SID=student.SID -- on 先判断条件然后得到查询结果
group by SID
having avg(sc.score)>=60 -- having 聚集函数之后
order by SID;

-- 方法2:
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  -- where 从查询结果里筛选
group by a.SID , a.Sname
having cast(avg(b.score) as decimal(18,2)) >= 60 -- having 聚集函数之后
order by a.SID

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!