SQL 交集 差集 並集 笛卡爾積 應用例項

2022-04-01 15:41:59 字數 4134 閱讀 6001

一、 交集 join

1、inner join 其處理結果與等值/自然連線相同  

mssql :      

--

mssql 中 inner join 連線需要 on connection條件 否則會報錯, mysql 不會,其結果等於cross join

select

*from major m inner

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno

mysql:

--其結果等於 crose join

select

*from major m inner

join

department d ;

--等值連線

select m.*,d.*

from major m inner

join department d on m.dptid = d.dptid;

2、 left join /left outer join 左連線/左外連線    

注:在sql 中, left join 為 left outer join 的縮寫    

mssql:

select

*from major m left

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =d.depnoorsubjectno

mysql:

select

*from major m left

join `department` d on m.dptid = d.dptid;

3、 right join/right outer join 右連線/右外連線   

mssql:    

select

*from major m right

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =d.depnoorsubjectno

mysql:    

select

*from major m right

outer

join `department` d on m.dptid = d.dptid;

4、full join 全連線

mssql:

--

a select

*from major m full

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno --b

select

*from major m left

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno

union

select

*from major m right

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno

--c

select

*from major m left

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno

union

allselect

*from major m right

outer

join

dbo.depnmorsubjectnm d

on m.depnoorsubjectno =

d.depnoorsubjectno

where m.depnoorsubjectno is

null

mysql:    

注:mysql 5.0版本還不支援 full join ,但可以採用 join + union方法實現,詳細可以參考:    

--

全連線 == 乙個左連線 + 乙個右連線 + union(去掉相同行) --a

select

*from major m left

outer

join `department` d on m.dptid =

d.dptid

union

select

*from major m right

outer

join `department` d on m.dptid =

d.dptid --b

select

*from major m left

outer

join `department` d on m.dptid =

d.dptid

union

allselect

*from major m right

outer

join `department` d on m.dptid =

d.dptid

where m.dptid is

null

--大力提倡使用b,尤其是在處理很大的記錄集時, union all 不會進行排序及消除相同的行(消除相同的行可能通過第二個join的條件進行實現),所以可以節省不少時間.

二、差集 (not in)

select

*from major m where m.dptid not

in(select dptid from department )

差集中, mysql 與 mssql 語句可以直接採用 not in 來實現    

三.、並集 union

mssql:    

--

union 並, 預設取消相同 行

select

*from

major m

union

select

*from

major tm

--union full 會有重覆記錄

select

*from

major m

union

allselect

*from major tm

mysql:

--

union 缺省會取消重複選項

select

*from

major `major`

union

select

*from

major mj ;

--union all 不會取消重複選項

select

*from

major m

union

allselect

*from major tm

四、 笛卡爾積

mssql:    

--

笛卡爾積

select

*from major cross

join

dbo.depnmorsubjectnm

mysql:    

--

笛卡爾集

select

*from major m cross

join `department` d;

帶條件的笛卡爾積與等值連線功能相同

SQL 交集,差集,並集,笛卡爾積

1 交集 join 1 inner join 其處理結果與等值 自然連線相同 如mssql mssql 中 inner join 連線需要 on connection條件 否則會報錯,mysql 不會,其結果等於cross join select from major m inner join db...

並集 交集 差集 Sql總結

1.並集 將查詢出的兩個結果合併成乙個結果集 union 去重,合併後的結果都是唯一 union all不去重,合併後的結果有可能出現重複的 oracle mysql sql server都支援下面的並集查詢 select classid from student union select clas...

交集並集差集

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...