all_student表是所有的学生信息,fail_student表是不及格的学生信息
select * from all_student
![](https://img.haomeiwen.com/i20803889/40f1bca68dcfc466.png)
select * from fail_student
![](https://img.haomeiwen.com/i20803889/834f77ae9449a52c.png)
查询及格的学生信息
select * from all_student
where name not in (
select name from fail_student
)
![](https://img.haomeiwen.com/i20803889/4da35d197428bb38.png)
假如fail_student有一条name为null的记录
![](https://img.haomeiwen.com/i20803889/c75c5f88c8135bee.png)
再次查询及格的学生信息,结果集为空
select * from all_student
where name not in (
select name from fail_student
)
![](https://img.haomeiwen.com/i20803889/a394dc959c669593.png)
解决方案
1.修改表结构,设置name字段为not null,并设置默认值
2.修改sql为
select * from all_student a
left join fail_student f on a.name = f.name
where f.name is null
![](https://img.haomeiwen.com/i20803889/8c107830d2f42389.png)
网友评论