当前位置:首页 » 编程语言 » sql表交集

sql表交集

发布时间: 2023-05-29 05:40:25

❶ 求多个表交集的sql语句是什么呀

使用 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.city

city 列中 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)

❷ 如何使用SQL语句求出交集

求交集嫌漏团的关键搜没字是 intersect ,例芹橘:
select * from emp where deptno in (10,20)
intersect
select * from emp where deptno in (20,30);

❸ sql如何取交集

select distinct id from a where id='123' and id in (select distinct id from a where id='456')

不过偶实在没看出select distinct id from a where id='123'这种语句有什么用处。。。。 就你写的来说这二者不可能有什么交集。

如果你的意思是指并集,就应该用select distinct id from a where id='456' or id = '123'

❹ 关于sql语句查询两个表的交集问题,谢谢!

select表1.id,表1.name,表1.sex,表2.difrom表1innerjoin表2on表1.name=表2.name

❺ sql如何查询两个表的交集

首先俩个表要存在关联关系,例:表A中的ID列和表B中的ID列是一样的数据,且唯一

则:

select * from A

left jion B on A.ID=B.ID


❻ SQL集合运算:差集、交集、并集



SQL集合运算:差集、交集、并集

2011年03月30日 15:41:00

阅读数:15446

1、差集( except )

select a from t_a

except

select a from t_b

-- 也可写作:

select a from t_a where a not in (select a from t_b)

-- 多个字段时:

select a,b from t_a

except

select a,b from t_b

-- 多字段的查集也可写成:

select a,b from t_a where (a,b) not in (select a,b from t_b)

2、交集( intersect )

select a from t_a

intersect

select a from t_b

-- 也可写作:

   select a from t_a where a in (select a from t_b)

3、并集( union )

select a from t_a

union distinct

select a from t_b

热点内容
张艺谋我们一家访问人 发布:2024-05-05 12:38:05 浏览:111
美版安卓系统怎么安装 发布:2024-05-05 12:37:18 浏览:920
qq邮箱缓存地址 发布:2024-05-05 12:37:16 浏览:986
电位算法 发布:2024-05-05 12:36:01 浏览:727
我的世界清风斗罗大陆服务器地址 发布:2024-05-05 12:35:50 浏览:453
dell服务器如何进入bios 发布:2024-05-05 12:34:26 浏览:330
在线名片制作源码 发布:2024-05-05 12:29:27 浏览:447
阴阳师按键脚本 发布:2024-05-05 12:00:33 浏览:760
魔兽查脚本 发布:2024-05-05 11:54:37 浏览:39
sqlserver执行时间 发布:2024-05-05 11:50:31 浏览:649