---
title: "exists,in,not exists,not in"
author: "oyprunner"
date: "2010-02-25T13:21:05Z"
tags: []
categories: ["数据库"]
slug: "exists-in-not-exists-not-in"
draft: false
lastmod: "2010-02-25T17:46:48Z"
id: 602779
url: "https://oyprunner.iteye.com/blog/602779"
---

**1 exists && in **
    

```sql
  --EXISTS的执行流程        
  select * from t1 where exists ( select null from t2 where y = x 
  --可以理解为:
    for x in ( select * from t1 )
     loop
        if ( exists ( select null from t2 where y = x.x )
        then 
           OUTPUT THE RECORD
        end if
     end loop
```

    **1.2  in**
  -----------------------------------------------------------
  --IN的执行流程
  select * from T1 where x in ( select y from T2 )
  --可以理解为:
  select * from t1, ( select distinct y from t2 ) t2 where t1.x = t2.y;
**2 not in &&  not exists**
    **2.1 not in**
   1,例子：从A表中查出来一个集合，从B表中查出来一个集合，且A表中包含了B表中的集合，求A表中不包含B表的集合

```sql
select a.iid from A a where a.iid not in(select iid from B)  
```

    **2.2 not exists**