sql替换NOT IN语句
来源:互联网 发布:数据库王珊第5版答案 编辑:程序博客网 时间:2024/05/29 05:05
进来项目中用到很多sql的语句,其中有个需求:
有两个表table1、table2,需要找出table1中存在,而table2中不存在的数据。要求不能使用NOT IN语句。
举例:两表的数据如下:
现在要从table1中选出记录,记录的f3字段不在table2中出现。
一、简单的使用NOT IN
select f1,f2,f3 from table1 where f3 not in(select f from table2);得到结果:
二、不用NOT IN
select res.f1,res.f2 from (select f1,f2,table2.f as tmp_colum from table1 left outer join table2 on table1.f3=table2.f) as res where res.tmp_colum is null;
使用left outer join来完成所需任务,其中的语句
select f1,f2,table2.f as tmp_colum from table1 left outer join table2 on table1.f3=table2.f结果为:
结果不言而喻,在在此基础上进行的筛选,选出两者交际为空的,就是table1不在table2 中的结果集。
0 0
- android开发技术博客
- PostgreSQL学习手册(客户端命令<二>)
- Hibernate的二级缓存
- hdu5009Paint Pearls dp
- mysql 横表和纵表转换
- sql替换NOT IN语句
- 秒杀多线程第六篇---经典线程同步 事件Event
- nyoj--914--Yougth的最大化(二分查找)
- webpy源码阅读
- codeforces-441B-Valera and Fruits【暴力】
- 关于平方和立方和,注意范围的取值顺序
- sicily 1125. Arnie versus the IRS
- ios swift UILabel 颜色使用图片
- Job任务的3种实现:java.util.Timer, spring Quartz,spring @scheduled