【数据库】|子查询

子查询

什么叫子查询?如何实现子查询?

定义:什么叫子查询,也就是先执行子查询,后执行父查询

❓✅面试题:如何实现替换,执行顺序?

1、使用子查询,因为子查询会先执行子句,在执行父句,效率非常低

sql 复制代码
select * from student where sid = (select max(sid) from student)
  • 查询id最大的一个学生(子查询)
    select max(sid) from student ,select * from student where sid = 9如何将这两行合并为一行sql进行执行呢?

    sql 复制代码
    select * from student where sid = (select max(sid) from student) 
  • 查询每个班级下面id最大的学生?

    sql 复制代码
    select * from student 
    left join class on student.classid = class.classid
    where sid in(
    select max(sid) from student group by classid
    )
  • 查询张三老师教了哪些学生?

    sql 复制代码
    select * from student where sid in(
    select sid from sc where cid = (
    	select cid from course where tid =(
    		(select tid from teacher where tname = '张三')
    )
    )
    )
  • ✅面试题目:请查询没有学习过张三老师的课的学生?

    • 思想
    1. 找出上了张三老师课程的学生❗
    2. 然后使用非❗
    3. 使用not in
    sql 复制代码
    select * from student where sid not in(
    select sid from sc where cid = (
    	select cid from course where tid =(
    		(select tid from teacher where tname = '张三')
    )
    )
    )

from子查询

定义: 查询结果作为一张表进行使用

  • 查询大于5 人的班级的名称和人数

    • 使用group by 进行解决
    sql 复制代码
    select * from class
    left join student on class.classid = student.classid
    GROUP BY class.classid having count(*) > 5
    • 使用子查询进行
    sql 复制代码
    -- 使用子查询
    -- 班级和学生进行了连接,进行了查询
    select classname , 人数 from class left join
    -- 查询每个班级有多少个人,然后在将班级表的classid和学生表的classid进行连接
    	(select classid,count(*) 人数
    	from student group by classid
    	)t1
    on class.classid = t1.classid
    where 人数 > 5

exist子查询

👀:定义:exist 子查询 子句有结果,父句执行,子句没有结果,父句不执行

sql 复制代码
select * from teacher
where exists (select * from student where classid = 10)

any some or 子查询

  • 查出一班成绩比二班最低成绩高的学生
sql 复制代码
select DISTINCT student.* from sc left join student on sc.Sid = student.Sid
where student.classid = 1 and score > some (select min(score) from sc left join student on sc.Sid = student.Sid
where student.classid = 2 )
  • 查询一般比二班最高的成绩还要高的学生,all满足所有的条件是and
sql 复制代码
select DISTINCT student.* from sc
left join student on sc.Sid = student.Sid
where student.classid = 1 and score > all (
select max(score) from sc
left join student on sc.Sid = student.Sid
where student.classid = 2 )

流程控制函数

1️⃣if

格式:

  1. if(value1, value2, value3)
  2. vaue1:条件成立
  3. value2:条件成立,显示数据
  4. value3:条件不成立 ,显示数据
  • 结果集的控制如果为1,则显示女,否则显示男
sql 复制代码
select * from teacher
select tid, tname, if(tsex=1,'女','男')tsex, Tbirthday,Taddress from teacher

2️⃣idnull (expres1, expre2)

格式:

  1. expre1 :字段
  2. expre2 :当字段值为null时,,默认值
  • 如果有生日,则显示原来的生日,如果没有生日,则显示默认值
sql 复制代码
select * from student
select sid, IFNULL(birthday,'我是石猴子'), sname from student

👍面试⭐️ case when then end

  • 当有0 时候显示男,当有1 的时候显示女
sql 复制代码
select tid, tname,
case tsex 
	when 0 then '男'
	when 1 then '女'
end tsex, tbirthday from teacher
  • 如果出现没有指定的性别,直接是一个null

    sql 复制代码
    select tid, tname,
    case tsex 
    	when 0 then '男'
    	when 1 then '女'
    	else '保密'
    end tsex, tbirthday from teacher  
  • 第二种写法

    sql 复制代码
    select tid,tname, 
    case
     when tsex > 1 then '男'
     when tsex = 1 then '女'
     when Tsex < 1 then '位置'
     END
    , tbirthday
    from teacher
  • 查询学生的所有成绩进行排名输出

sql 复制代码
select 
case 
when score >= 90 then 'A'
when score >= 80 then 'B'
when score > 70 then 'C'
when score > 60 then 'D'
when score < 60 then '不及格'
else '无成绩'
end
成绩 from sc
  • -- 统计各个分数段的人数 ,分数段 人数
SQL 复制代码
select 
case 
when score >= 90 then '100-90'
when score >= 80 then 'B'
when score > 70 then 'C'
when score > 60 then 'D'
when score < 60 then '不及格'
else '无成绩'
end
分数段 from sc

✅面试题目:给你一个页面让你写另一个页面

sql 复制代码
给出的页面
-- 面试题目
select '100-90', score , count(score) 人数 from sc where score > 90 and score <100
union 
select '90-70', score , count(score) 人数 from sc where score > 70 and score <90
union 
select '70-60', score , count(score) 人数 from sc where score > 60 and score <70
union
select '不及格', score , count(score) 人数 from sc where score <60

如何实现到这个步骤?

sql 复制代码
select '人数' 分数,
	count(case when score <= 100 and score >=90 then score end)'100-90',
	count(case when score <= 90 and score >=90 then score end)'90-70',
	count(case when score <= 70 and score >=90 then score end)'70-60',
	count(case when score <= 60 and score >=90 then score end)'不及格'
 from sc
相关推荐
库库林_沙琪马1 小时前
Redis 持久化:从零到掌握
数据库·redis·缓存
牵牛老人2 小时前
Qt中使用QPdfWriter类结合QPainter类绘制并输出PDF文件
数据库·qt·pdf
卡西里弗斯奥4 小时前
【达梦数据库】dblink连接[SqlServer/Mysql]报错处理
数据库·mysql·sqlserver·达梦
温柔小胖5 小时前
sql注入之python脚本进行时间盲注和布尔盲注
数据库·sql·网络安全
杨俊杰-YJ5 小时前
MySQL 主从复制原理及其工作过程
数据库·mysql
一个儒雅随和的男子6 小时前
MySQL的聚簇索引与非聚簇索引
数据库·mysql
V+zmm101348 小时前
基于微信小程序的家政服务预约系统的设计与实现(php论文源码调试讲解)
java·数据库·微信小程序·小程序·毕业设计
roman_日积跬步-终至千里8 小时前
【分布式理论14】分布式数据库存储:分表分库、主从复制与数据扩容策略
数据库·分布式
hadage2338 小时前
--- Mysql事务 ---
数据库·mysql
-$_$-9 小时前
【黑马点评优化】2-Canel实现多级缓存(Redis+Caffeine)同步
数据库·redis·缓存