mysql6.10,MySQL经典50题-第6-10题

MySQL50-4-第6-10题

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

模糊匹配和通配符使用

表的自连接

in/not in

连接查询的条件筛选

3da1a621acc7

image

题目6

题目需求

查询“李”姓老师的数量

分析过程

使用通配符和like来解决

SQL实现

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

3da1a621acc7

image

这题怕是最简单的吧😭

题目7

3da1a621acc7

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='张三';

自己的方法:

3da1a621acc7

image

方法2来实现:

3da1a621acc7

image

方法3实现:

3da1a621acc7

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="张三"

)

);

3da1a621acc7

image

方法2:

3da1a621acc7

image

题目9

3da1a621acc7

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);

先从Score表中看看哪些人是满足要求的:01-05同学是满足的

3da1a621acc7

image

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

3da1a621acc7

image

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

3da1a621acc7

image

通过where语句实现:

3da1a621acc7

image

方法3的实现:

3da1a621acc7

image

题目10

题目需求

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

分析过程

使用的表和字段是相同的:

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

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

SQL实现

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

3da1a621acc7

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'

);

3da1a621acc7

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)的同学也会出现

);

3da1a621acc7

image

正确思路

-- 方法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,需要排除

3da1a621acc7

3da1a621acc7

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')

3da1a621acc7

image

3da1a621acc7

image

如何找出06号学生😃

如何Score中找出06号学生

3da1a621acc7

image

3da1a621acc7

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值