sql server 交集,差集的用法詳解

2021-10-08 11:07:28 字數 2960 閱讀 8364

為什麼使用集合運算:

在集合運算中比聯接查詢和exists/not exists更方便。

並集運算(union)

並集:兩個集合的並集是乙個包含集合a和b中所有元素的集合。

在t-sql中。union集合運算可以將兩個輸入查詢的結果組合成乙個結果集。需要注意的是:如果乙個行在任何乙個輸入集合**現,它也會在union運算的結果**現。t-sql支援以下兩種選項:

(1)union all:不會刪除重複行12

34-- union allselect 

country, region, city from hr.employees

union all

select country, region, city from sales.customers;

(2)union:會刪除重複行12

34-- union

select country, region from hr.employees

union

select country, region from sales.customers;

交集運算(intersect)

交集:兩個集合(記為集合a和集合b)的交集是由既屬於a,也屬於b的所有元素組成的集合。

在t-sql中,intersect集合運算對兩個輸入查詢的結果取其交集,只返回在兩個查詢結果集中都出現的行。

intersect集合運算在邏輯上會首先刪除兩個輸入集中的重複行,然後返回只在兩個集合中中都出現的行。換句話說:如果乙個行在兩個輸入集中都至少出現一次,那麼交集返回的結果中將包含這一行。12

34-- intersect

select country, region, city from hr.employees

intersect

select country, region, city from sales.customers;

這裡需要說的是,集合運算對行進行比較時,認為兩個null值相等,所以就返回該行記錄。

差集運算(except)

差集:兩個集合(記為集合a和集合b)的由屬於集合a,但不屬於集合b的所有元素組成的集合。

在t-sql中,集合之差使用except集合運算實現的。它對兩個輸入查詢的結果集進行操作,反會出現在第乙個結果集中,但不出現在第二個結果集中的所有行。

except結合運算在邏輯上首先刪除兩個輸入集中的重複行,然後返回只在第乙個集合**現,在第二個結果集中不出現的所有行。換句話說:乙個行能夠被返回,僅當這個行在第乙個輸入的集合中至少出現過一次,而且在第二個集合中一次也沒出現過。

此外,相比union和intersect,兩個輸入集合的順序是會影響到最後返回結果的。12

34-- except 

select country, region, city from hr.employees

except

select country, region, city from sales.customers;

集合運算優先順序

sql定義了集合運算之間的優先順序:intersect最高,union和except相等。

換句話說:首先會計算intersect,然後按照從左至右的出現順序依次處理優先順序相同的運算。12

3456

-- 集合運算的優先順序

select country, region, city from production.suppliers

except

select country, region, city from hr.employees

intersect

select country, region, city from sales.customers;

上面這段sql**,因為intersect優先順序比except高,所以首先進行intersect交集運算。因此,這個查詢的含義是:返回沒有出現在員工位址和客戶位址交集中的**商位址。

集合運算的優先順序

1.intersect>union=except

2.首先計算intersect,然後從左到右的出現順序依次處理優先順序的相同的運算。

3.可以使用圓括號控制集合運算的優先順序,它具有最高的優先順序。

在排序函式的over字句中使用order by ( select 《常量》 )可以告訴sql server不必在意行的順序。

使用表表示式避開不支援的邏輯查詢處理

集合運算查詢本身並不持之除order by意外的其他邏輯查詢處理階段,但可以通過表表示式來避開這一限制。

解決方案就是:首先根據包含集合運算的查詢定義乙個表表示式,然後在外部查詢中對錶表示式應用任何需要的邏輯查詢處理。12

34select country, count(*) as numlocations

from (select country, region, city from hr.employees 

union 

select country, region, city from sales.customers) as ugroup by country;

(2)例如,下面的查詢返回由員工位址為3或5的員工最近處理過的兩個訂單:、12

3456

78910

11select empid,orderid,orderdate 

from (select top (2) empid,orderid,orderdate 

from sales.orders

where empid=3

order by orderdate desc,orderid desc) as d1

union all

select empid,orderid,orderdate 

from (select top (2) empid,orderid,orderdate 

from sales.orders

where empid=5

order by orderdate desc,orderid desc) as d2;

交集並集差集

1 內連線 select from student a inner join sc b on a.sno b.sno 左連線 select from student a left join sc b on a.sno b.sno 差集 select sno from student except s...

golang交集,差集

從今天起寫一些golang 函式實現php庫函式的功能 php 函式 array diff,array intersect package php arraydiff 模擬php array diff函式 func arraydiff array1 inte ce othersparams inte...

java set 的交集,並集,差集

近日做專案,有個需求是這樣的,有個map1,map2,這兩個map的key有可能是重複的,如果有重複的key,則找出重複的key,然後刪除map1中重複的key以及value,以map2為準,但是map1下面還有用處。然後想到用set求交集不就好了,於是這樣做了 set set1 map1.keys...