oracle中的exists 和not exists 用法详解
来源:互联网 发布:万网域名注册阿里云 编辑:程序博客网 时间:2024/06/05 14:25
oracle中的exists 和not exists 用法详解
1) select * from T1 where exists(select 1 from T2 whereT1.a=T2.a) ;
2) select * from T1 where T1.a in (select T2.a from T2) ;
exists 用法:
请注意 1)句中的有颜色字体的部分 ,理解其含义;
其中 “select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于
“select 1 fromT1,T2
但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。
“exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。
因此“select 1”这里的“1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where条件成立。
in 的用法:
继续引用上面的例子
“2) select * from T1 where T1.a in (select T2.a from T2) ”
这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。
打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用2)的写法就可以这样:
“select * from T1 where T1.ticketid in (select T2.id from T2)”
Select name from employee where name not in (select name fromstudent);
Select name from employee where not exists (select name fromstudent);
第一句SQL语句的执行效率不如第二句。
通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因
- oracle中的exists和not exists和in用法详解
- oracle中的exists和not exists和in用法详解
- oracle中的exists和not exists和in用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- oracle中的exists 和not exists 用法详解
- Oracle中的EXISTS 和NOT EXISTS 用法详解
- c++语言 运算符重载 使用重载运算符实现类的加法运算
- 简单的ABAP面相对象实例
- Creditmetrics模型
- 第二章、关系数据库
- Git 常用命令
- oracle中的exists 和not exists 用法详解
- iPhone开发之深入浅出 (6) — ARC之对象转型
- JAVA,IO性能优化——缓冲区
- sqlserver中把表数据变成插入语句
- Timer与ScheduledExecutorService 的使用和区别
- Linux编程基础1 GCC程序编译
- 实战 SQL Server 2008 数据库误删除数据的恢复
- linux下安装jdk1.7
- .显示表结构的存储过程