sql求兩表的並集 交集 非交集 差集 結果集排序

2022-06-15 20:36:18 字數 1946 閱讀 2153

create

table

a( id

intidentity(1,1) not

null

primary

key,

name

varchar(20) not

null

default(''),)

insert

into[a

]([name

]) values('a'

)insert

into[a

]([name

]) values('b'

)insert

into[a

]([name

]) values('c'

)insert

into[a

]([name

]) values('d'

)insert

into[a

]([name

]) values('e'

)insert

into[a

]([name

]) values('f'

)insert

into[a

]([name

]) values('g'

)create

table

b( id

intidentity(1,1) not

null

primary

key,

name

varchar(20) not

null

default(''),)

insert

into[b

]([name

]) values('a'

)insert

into[b

]([name

]) values('b'

)insert

into[b

]([name

]) values('c'

)insert

into[b

]([name

]) values('d'

)insert

into[b

]([name

]) values('h'

)insert

into[b

]([name

]) values('i'

)insert

into[b

]([name

]) values('

j')

select

*from a union

select

*from b --

查詢a\b表的並集重複的項只顯示乙個

select

*from a union

allselect

*from b --

--查詢a\b表的並集重複的也顯示

select

*from a union

allselect

*from b order

by id asc

---查詢a\b表的並集重複的也顯示,並按照id公升序

select

*from a intersect

select

*from b --

--查詢兩表的交集

select

*from a except

select

*from b --

--查詢a表中不與b表重複的記錄

--查詢a\b兩表中所有非交集的記錄

(select

*from a except

select

*from b) union (select

*from b except

select

*from a)

並集 交集 差集 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...

順序表的交集 並集 差集

using namespace std define maxsize 20 define true 1 define false 0 typedef bool status status是函式的型別,其值是函式結果狀態 typedef char elemtype elemtype型別根據實際情況而定...