1.前言:
我们知道子查询可以放在from后,join后,where xx not in后,还可以直接放在select后,今天我们实战几个子查询之not in,来加深一下not in的使用;
2.实战
案例一:有这样一张stu_score表,字段有三个,分别是name 名字 course 课程 score 分数
需求:我们现在需要查出每门功课成绩都大于80分的学生的姓名
name course score
张三 语文 81
张三 数学 75
张三 英语 70
李四 语文 76
李四 数学 90
李四 英语 75
王五 语文 81
王五 英语 90
王五 数学 100
实现:
方法一:
select distinct name from stu_score where name not in (select distinct name from stu_score where score < 80);
方法二:
select name from stu_score group by name having min(score)> 80;
案例二:有这样一张stu_info表,字段有四个,分别是num 编号 name 名字 course 课程 score 分数
需求:删除除了编号不同,其他都相同的学生溶于信息;
num name course score
1 张三 数学 60
2 李四 数学 89
3 王五 英语 80
4 张三 数学 60
实现:
方法一:
delete stu_info where num not in (select min(num) from stu_info group by name,course,score );