美文网首页转载部分数据库
MySQL经典50题-第6-10题

MySQL经典50题-第6-10题

作者: 皮皮大 | 来源:发表于2021-03-16 10:44 被阅读0次

MySQL50-4-第6-10题

本文中介绍的是第6-10题,涉及到的主要知识点:

  • 模糊匹配和通配符使用

  • 表的自连接

  • in/not in

  • 连接查询的条件筛选

image

题目6

题目需求

查询“李”姓老师的数量

分析过程

使用通配符和like来解决

SQL实现

select count(t_name) from Teacher where t_name like "李%";   -- 通配符
image

这题怕是最简单的吧😭

题目7

image

题目需求

查询学过张三老师授课的同学的信息

分析过程

张三老师:Course--->t_name
课程:c_id------>Score.c_id------->Student.*

SQL实现

-- 方法1:通过张三老师的课程的学生来查找;自己的方法
select * -- 3. 通过学号找出全部学生信息
from Student
where s_id in (
  select s_id    -- 2.通过课程找出对应的学号
  from Score  S
  join Course C
  on S.c_id = C.c_id  -- 课程表和成绩表
  where C.t_id=(select t_id from Teacher where t_name="张三")  -- 1.查询张三老师的课程
);

-- 方法2:通过张三老师的课程来查询
select s1.* 
from Student s1
join Score s2 
on s1.s_id=s2.s_id 
where s2.c_id in (
  select c_id from Course c where t_id=(  -- 1. 通过老师找出其对应的课程
    select t_id from Teacher t where t_name="张三"
  )
)

-- 方法3
select s.* from Teacher t
left join Course c on t.t_id=c.t_id  -- 教师表和课程表
left join Score sc on c.c_id=sc.c_id  -- 课程表和成绩表
left join Student s on s.s_id=sc.s_id  -- 成绩表和学生信息表
where t.t_name='张三';

自己的方法:

image

方法2来实现:

image

方法3实现:

image

题目8

题目需求

找出没有学过张三老师课程的学生

分析过程

和上面👆的题目是互补的,考虑取反操作

SQL实现

select * -- 3. 通过学号找出全部学生信息
from Student
where s_id not in (  -- 2.通过学号取反:学号不在张三老师授课的学生的学号中
  select s_id    
  from Score  S
  join Course C
  on S.c_id = C.c_id
  where C.t_id=(select t_id from Teacher where t_name ="张三")  -- 1.查询张三老师的课程
);

-- 方法2:
select * 
from Student s1
where s1.s_id not in (
  select s2.s_id from Student s2 join Score s3 on s2.s_id=s3.s_id where s3.c_id in(
    select c.c_id from Course c join Teacher t on c.t_id=t.t_id where t_name="张三" 
  )
);

-- 方法3
select s1.* 
from Student s1
join Score s2 
on s1.s_id=s2.s_id 
where s2.c_id not in (
  select c_id from Course c where t_id=(  -- 1. 通过老师找出其对应的课程
    select t_id from Teacher t where t_name="张三"
  )
);
image

方法2:

image

题目9

image

题目需求

查询学过编号为01,并且学过编号为02课程的学生信息

分析过程

  • 课程编号:Score——>c_id(课程编号)

  • 学生信息:Student——>*(学生信息)

SQL实现

-- 自己的方法:通过自连接实现
select s1.*
from Student s1
where s_id in (
  select s2.s_id from Score s2
  join Score s3
  on s2.s_id=s3.s_id
  where s2.c_id='01' and s3.c_id='02'
);

-- 方法2:直接通过where语句实现
select s1.*
from Student s1, Score s2, Score s3
where s1.s_id=s2.s_id 
and s1.s_id=s3.s_id
and s2.c_id=01 and s3.c_id=02;

-- 方法3:两个子查询
-- 1. 先查出学号
select sc1.s_id
from (select * from Score s1 where s1.c_id='01') sc1,
            (select * from Score s1 where s1.c_id='02') sc2
where sc1.s_id=sc2.s_id;

-- 2.找出学生信息
select * 
from Student 
where s_id in (select sc1.s_id   -- 指定学号是符合要求的
               from (select * from Score s1 where s1.c_id='01') sc1,
               (select * from Score s1 where s1.c_id='02') sc2
where sc1.s_id=sc2.s_id);
  1. 先从Score表中看看哪些人是满足要求的:01-05同学是满足的
image

通过自连接查询的语句如下:

image

查询出学号后再匹配出学生信息:

image

通过where语句实现:

image

方法3的实现:

image

题目10

题目需求

查询学过01课程,但是没有学过02课程的学生信息(注意和上面👆题目的区别)

分析过程

使用的表和字段是相同的

  • 课程编号:Score——>c_id(课程编号)
  • 学生信息:Student——>*(学生信息)

SQL实现

首先看看哪些同学是满足要求的:只有06号同学是满足的

image
错误思路1

直接将上面一题的结果全部排出,导致那些没有学过01课程的学生也出现了:07,08

select s1.*
from Student s1
where s_id not in (   -- 直接将上面一题的结果全部排出,导致那些没有学过01课程的学生也出现了:07,08
  select s2.s_id from Score s2
  join Score s3
  on s2.s_id=s3.s_id
  where s2.c_id='01' and s3.c_id ='02'
);
image
错误思路2

将上面题目中的02课程直接取反,导致同时修过01,02,03或者只修01,03的同学也会出现

select s1.*
from Student s1
where s_id in (
  select s2.s_id from Score s2
  join Score s3
  on s2.s_id=s3.s_id
  where s2.c_id='01' and s3.c_id !='02'   -- 直接取反是不行的,因为修改(01,02,03)的同学也会出现
);
image
正确思路

https://www.jianshu.com/p/9abffdd334fa

-- 方法1:根据两种修课情况来判断

select s1.* 
from Student s1
where s1.s_id in (select s_id from Score where c_id='01')   -- 修过01课程,要保留
and s1.s_id not in (select s_id from Score where c_id='02');  -- 哪些人修过02,需要排除
image

!!!!!方法2:先把06号学生找出来

 select * from Student where s_id in (
  select s_id 
  from Score 
  where c_id='01'   -- 修过01课程的学号
  and s_id not in (select s_id   -- 同时学号不能在修过02课程中出现
  from Score 
  where c_id='02')
 );
select s_id 
 from Score 
 where c_id='01'   -- 修过01课程的学号
 and s_id not in (select s_id   -- 同时学号不能在修过02课程中出现
  from Score 
  where c_id='02')
image image

如何找出06号学生😃

如何Score中找出06号学生

image image

相关文章

  • MySQL经典50题-第6-10题

    MySQL50-4-第6-10题 本文中介绍的是第6-10题,涉及到的主要知识点: 模糊匹配和通配符使用 表的自连...

  • MySQL经典50题-第16到20题

    MySQL50-6-第16-20题 本文中介绍的是第16-20题,涉及到的知识点包含: 自连接 SQL实现排序 多...

  • MySQL经典50题-第11-15题

    MySQL50-5-第11-15题 本文中介绍的是第11-15题,具体的题目包含: 查询没有学完全部课程的同学的信...

  • MySQL经典50题-第1-5题

    MySQL经典50题-3-第1-5题目 本文中介绍的是1-5题,从题目和答案两个方面进行记录,涉及到的知识点: 一...

  • MySQL经典50题-第26到30题

    本文中介绍的是第26-30题目,主要涉及的知识点是: 分组之后count统计人数 模糊匹配 同一个表的自连接 ha...

  • MySQL经典50题-第21到25题

    MySQL50-7-第21-25题 本文中介绍的是第21-25题目,主要涉及的知识点是: 分组统计求和,百分比 如...

  • MySQL经典50题-第31到35题

    MySQL50-9-第31-35题 本文中介绍的是第31-35题目,主要涉及的知识点是: 模糊匹配 同时指定多种排...

  • MySQL经典50题-第36到40题

    MySQL50-10-第36-40题 本文中介绍的是第36-40题目,涉及到的知识点都是多表的连接查询,需要指定不...

  • MySQL经典50题-第41到45题

    MySQL50-11-第41-45题 本文中介绍的是第41-45题,主要包含的知识点: 表的自连接查询比较信息 找...

  • 完结篇!MySQL经典50题-第46到50题

    MySQL50-12-第46-50题 本文中介绍的是第46-50题,主要的知识点:各种时间和日期函数的使用 yea...

网友评论

    本文标题:MySQL经典50题-第6-10题

    本文链接:https://www.haomeiwen.com/subject/osgrcltx.html