【数据库】|子查询

子查询

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

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

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

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
相关推荐
白鹭39 分钟前
MySQL源码部署(rhel7)
数据库·mysql
666和7771 小时前
Struts2 工作总结
java·数据库
还听珊瑚海吗1 小时前
SpringMVC(一)
数据库
星期天要睡觉2 小时前
MySQL 综合练习
数据库·mysql
Y4090013 小时前
数据库基础知识——聚合函数、分组查询
android·数据库
JosieBook3 小时前
【数据库】MySQL 数据库创建存储过程及使用场景详解
数据库·mysql
处女座_三月4 小时前
改 TDengine 数据库的时间写入限制
数据库·sql·mysql
酷ku的森4 小时前
Redis中的hash数据类型
数据库·redis·哈希算法