---
title: "oracle  中 in 跟 exists 的用法"
author: "lhy2012"
date: "2012-04-21T14:50:55Z"
tags: []
categories: ["数据库"]
slug: "oracle-in-exists"
draft: false
lastmod: "2012-04-21T14:50:55Z"
id: 1493387
url: "https://lhy2012.iteye.com/blog/1493387"
---

例如  
Users 表
   account   name 
     1	   张三
      2	   李四
 phone 表 
    account  telphone
     1	    135678
     2	    132789 
1: exists 的用法
   Select account, name
    from users
 where exists (select account from phone where telephone like ‘135%’)
返回的结果是：
       Account   name 
       1          张三
使用EXISTS，Oracle会首先检查主查询，然后运行子查询直到它找到第一个匹配项
  2: in的用法
  Select account, name
  from users 
 where  account in (select account from phone where telephone like ‘135%’)
   Account   name 
    1          张三
Oracle在执行IN子查询时，首先执行子查询，并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前，系统先将主查询挂起，待子查询执行完毕，存放在临时表中以后再执行主查询。
两者之间的效率比较：
IN适合于外表大而内表小的情况；EXISTS适合于外表小而内表大的情况。