SQLSERVER collection processing - EXCEPT

        Sometimes we need to process two or more sets of data. At this time, we need several functions that come with SQL Server. Today we will introduce the usage of a function EXCEPT to find the difference between two sets (T1-T2).

        First upload the test data:

if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([Id] int,[Name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四'
GO
if not object_id(N'Tempdb..#T2') is null
	drop table #T2
Go
Create table #T2([Id] int,[Name] nvarchar(22))
Insert #T2
select 1,N'张三' union all
select 2,N'王五'
Go

        For the data of the two sets, we want to obtain the data that is in #T1 but not in #T2. At this time, the EXCEPT method is used. The usage method is as follows:

SELECT * FROM #T1
EXCEPT
SELECT * FROM #T2

        The results are as follows, and the data of Li Si is obtained:

Guess you like

Origin blog.csdn.net/sinat_28984567/article/details/123603572