SQLSERVER collection processing - INTERSECT

        The previous blog recorded the method of seeking T1-T2 https://blog.csdn.net/sinat_28984567/article/details/123603572  This time we will introduce the method INTERSECT for the intersection of two tables T1 and T2, 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

        We want to get the intersection data Zhang San in both tables. At this time, we can use the INTERSECT method, as follows:

SELECT * FROM #T1
INTERSECT
SELECT * FROM #T2

        The result is as follows, we get the intersection of the two tables Zhang San:

 

Guess you like

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