SELECT * FROM A T WHERE EXISTS(SELECT 1 FROM B WHERE T.ID=ID AND NAME=T.ID)SELECT * FROM A T WHERE ID IN(SELECT ID FROM B)EXISTS可以关联多个,IN 只能一个
SELECT * FROM A T WHERE EXISTS(SELECT 1 FROM B WHERE T.ID=ID AND NAME=T.ID)SELECT * FROM A T WHERE ID IN(SELECT ID FROM B)exists 与 in 是几乎完全一样的 ,但一般 用exists 比较好,因为假如 not in (查询A) 中的查询A 有null 值的话结果就是错误的
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。效率不一定谁高
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 GOB. 比较使用 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 = ANYUSE 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)
declare @小组表 table (小组编码 int,小组名称 varchar(1)) insert into @小组表 select 1,'a' union all select 2,'b' union all select 3,'c' union all select 4,'d' union all select 5,'e' union all select null,null union all select 7,'g' union all select 8,'h' union all select 9,'i' union all select 10,'j'declare @登记表 table (小组 int) insert into @登记表 select null union all select 3 union all select 4 union all select 5 union all select 6SELECT 小组编码,小组名称 FROM @小组表 a where not exists (select * from @登记表 b where b.小组=a.小组编码)/* 小组编码 小组名称 ----------- ---- 2 b 7 g 8 h 9 i 10 j */SELECT 小组编码,小组名称 FROM @小组表 where 小组编码 not in (select 小组 from @登记表) /* 小组编码 小组名称 ----------- ----(0 row(s) affected) */没有null时,结果一样。
in 是指某数据值在不在子查询的结果集中,如数据3在子查询结果集(3、6、9、12、15)之中则返回true,否则返回false,in 的左侧需要指定与右侧子查询结果进行比较运算的对象,而EXISTS 是指某个数据行在不在指定的结果行中,左侧不需要指定与右侧查询结果进行比较运算的对象,且右侧子查询也不返回实际数据,只是测试存在与否。存在的返回true,不然为false。不知这样理解是否正确?
查询null值时,in会出现短路,exists不会出现短路 use tempdb; /* create table test ( id int not null, name nvarchar(20) );insert into test(id,name) values (1,'admin'),(2,null),(3,'test'); */select * from test where test.name in (select name from test); select * from test where exists (select name from test);
另一个不同是,in只能用于一个字段,exists可以用于多个字段 select * from test where exists (select * from test);
not in (查询A) 中的查询A 有null 值的话结果就是错误的
指定一个子查询,检测行的存在。 语法
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
GOB. 比较使用 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 = ANYUSE 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)
declare @小组表 table (小组编码 int,小组名称 varchar(1))
insert into @小组表
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d' union all
select 5,'e' union all
select null,null union all
select 7,'g' union all
select 8,'h' union all
select 9,'i' union all
select 10,'j'declare @登记表 table (小组 int)
insert into @登记表
select null union all
select 3 union all
select 4 union all
select 5 union all
select 6SELECT 小组编码,小组名称
FROM @小组表 a where
not exists (select * from @登记表 b where b.小组=a.小组编码)/*
小组编码 小组名称
----------- ----
2 b
7 g
8 h
9 i
10 j
*/SELECT 小组编码,小组名称
FROM @小组表 where 小组编码 not in (select 小组 from @登记表)
/*
小组编码 小组名称
----------- ----(0 row(s) affected)
*/没有null时,结果一样。
use tempdb;
/*
create table test
(
id int not null,
name nvarchar(20)
);insert into test(id,name) values (1,'admin'),(2,null),(3,'test');
*/select * from test where test.name in (select name from test);
select * from test where exists (select name from test);
select * from test where exists (select * from test);