當前位置:首頁 » 編程語言 » 交集sql

交集sql

發布時間: 2023-07-21 01:21:11

sql如何查詢兩個表的交集

首先倆個表要存在關聯關系,例:表A中的ID列和表B中的ID列是一樣的數據,且唯一

則:

select * from A

left jion B on A.ID=B.ID


㈡ 求多個表交集的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集合運算:差集、交集、並集



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

㈣ 如何使用SQL語句求出交集

求交集嫌漏團的關鍵搜沒字是 intersect ,例芹橘:
select * from emp where deptno in (10,20)
intersect
select * from emp where deptno in (20,30);

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:624
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:355
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:69
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:294
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:786
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:336
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:201
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:796
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:353
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:581