从无到有

我本将心向明月,奈何明月照沟渠

导航

SQL中EXISTS的用法

EXISTS


指定一个子查询,检测行的存在。
语法

EXISTS subquery
参数
subquery

是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字)。有关更多信息,请参见 SELECT 中有关子查询的讨论。

结果类型

Boolean
结果值

如果子查询包含行,则返回 TRUE。
示例
A. 在子查询中使用 NULL 仍然返回结果集

这个例子在子查询中指定 NULL,并返回结果集,通过使用 EXISTS 仍取值为 TRUE。
USE Northwind
GO
SELECT CategoryName
FROM Categories
WHERE EXISTS (SELECT NULL)
ORDER BY CategoryName ASC
GO
B. 比较使用 EXISTS 和 IN 的查询

这个例子比较了两个语义类似的查询。第一个查询使用 EXISTS 而第二个查询使用 IN。注意两个查询返回相同的信息。
USE pubs
GO
SELECT DISTINCT pub_name
FROM publishers
WHERE EXISTS
   (SELECT *
   FROM titles
   WHERE pub_id = publishers.pub_id
   AND type = \'business\')
GO

-- Or, using the IN clause:

USE pubs
GO
SELECT distinct pub_name
FROM publishers
WHERE pub_id IN
   (SELECT pub_id
   FROM titles
   WHERE type = \'business\')
GO

下面是任一查询的结果集:
pub_name                                 
----------------------------------------
Algodata Infosystems                    
New Moon Books                          

(2 row(s) affected)
C.比较使用 EXISTS 和 = ANY 的查询

本示例显示查找与出版商住在同一城市中的作者的两种查询方法:第一种方法使用 = ANY,第二种方法使用 EXISTS。注意这两种方法返回相同的信息。
USE pubs
GO
SELECT au_lname, au_fname
FROM authors
WHERE exists
   (SELECT *
   FROM publishers
   WHERE authors.city = publishers.city)
GO

-- Or, using = ANY

USE pubs
GO
SELECT au_lname, au_fname
FROM authors
WHERE city = ANY
   (SELECT city
   FROM publishers)
GO

下面是任一查询的结果集:
au_lname                                 au_fname             
---------------------------------------- --------------------
Carson                                   Cheryl              
Bennet                                   Abraham              

(2 row(s) affected)
D.比较使用 EXISTS 和 IN 的查询

本示例所示查询查找由位于以字母 B 开头的城市中的任一出版商出版的书名:
USE pubs
GO
SELECT title
FROM titles
WHERE EXISTS
   (SELECT *
   FROM publishers
   WHERE pub_id = titles.pub_id
   AND city LIKE \'B%\')
GO

-- Or, using IN:

USE pubs
GO
SELECT title
FROM titles
WHERE pub_id IN
   (SELECT pub_id
   FROM publishers
   WHERE city LIKE \'B%\')
GO

下面是任一查询的结果集:
title                                                                            
------------------------------------------------------------------------
The Busy Executive\'s Database Guide                                              
Cooking with Computers: Surreptitious Balance Sheets                            
You Can Combat Computer Stress!                                                  
Straight Talk About Computers                                                    
But Is It User Friendly?                                                        
Secrets of Silicon Valley                                                        
Net Etiquette                                                                    
Is Anger the Enemy?                                                              
Life Without Fear                                                                
Prolonged Data Deprivation: Four Case Studies                                    
Emotional Security: A New Algorithm                                              

(11 row(s) affected)
E. 使用 NOT EXISTS

NOT EXISTS 的作用与 EXISTS 正相反。如果子查询没有返回行,则满足 NOT EXISTS 中的 WHERE 子句。本示例查找不出版商业书籍的出版商的名称:
USE pubs
GO
SELECT pub_name
FROM publishers
WHERE NOT EXISTS
   (SELECT *
   FROM titles
   WHERE pub_id = publishers.pub_id
   AND type = \'business\')
ORDER BY pub_name
GO

下面是结果集:
pub_name                                 
----------------------------------------
Binnet & Hardley                        
Five Lakes Publishing                    
GGG&G                                    
Lucerne Publishing                      
Ramona Publishers                        
Scootney Books                          

(6 row(s) affected)

posted on 2005-07-13 15:43  从无到有.NET  阅读(42587)  评论(7编辑  收藏  举报