select * from @t t
where not exists(select 1 from @c where @[email protected])
exists 到底是个什么意思。
听人讲返回什么true false 我还是不理解。
能不能说的详细点 这句话怎么在运行。
not exists(select 1 from @c where @[email protected])
我感觉结果是当限定条件在用啊 怎么是true false 呢
where not exists(select 1 from @c where @[email protected])
exists 到底是个什么意思。
听人讲返回什么true false 我还是不理解。
能不能说的详细点 这句话怎么在运行。
not exists(select 1 from @c where @[email protected])
我感觉结果是当限定条件在用啊 怎么是true false 呢
where not exists(select 1 from @c where @[email protected]) =select * from @t t
where no not in (select no from @c) 使用 EXISTS 和 NOT EXISTS 查找交集与差集
使用 EXISTS 和 NOT EXISTS 引入的子查询可用于两种集合原理的操作:交集与差集。两个集合的交集包含同时属于两个原集合的所有元素。差集包含只属于两个集合中的第一个集合的元素。city 列中 authors 和 publishers 的交集是作者和出版商共同居住的城市的集合。USE pubs
SELECT DISTINCT city
FROM authors
WHERE EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)下面是结果集:city
--------
Berkeley(1 row(s) affected)当然,该查询可以写成一个简单的联接。USE pubs
SELECT DISTINCT authors.city
FROM authors INNER JOIN publishers
ON authors.city = publishers.citycity 列中 authors 和 publishers 的差集是作者所居住的、但没有出版商居住的所有城市的集合,也就是除 Berkeley 以外的所有城市。USE pubs
SELECT DISTINCT city
FROM authors
WHERE NOT EXISTS
(SELECT *
FROM publishers
WHERE authors.city = publishers.city)该查询也可以写成:USE pubs
SELECT DISTINCT city
FROM authors
WHERE city NOT IN
(SELECT city
FROM publishers)
where
not exists --2.如果里面的查询结果为true,加上这个条件就是false(即排除@t表中与@c的no号相同的记录)
(
select 1 from @c where @[email protected] --1.查询结果不为空(即两个表的no有相同的),则为true
)
select * from @t t
where
not exists
(
select 1 from @[email protected]
)其实就是检验存不存在
where not exists(select 1 from @c where @[email protected]) 等同于:
select * from @t t
where t.no not in(select no from @c)