SQLSERVER collection processing - UNION

        We have introduced  the method of finding the difference set of two tables  and  the intersection of two tables before. Today we are introducing a method of finding the set of two tables. 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

        At this time, if we want to get the collection of two tables, we can use the UNION method, as follows:

SELECT * FROM #T1
UNION
SELECT * FROM #T2

        The result is as follows:

        From the above results, we can see that the collection of two tables is obtained, and the collection is automatically deduplicated, and Zhang San is removed. If you do not want to deduplicate, you can use UNION ALL to get a collection without deduplication:

SELECT * FROM #T1
UNION ALL
SELECT * FROM #T2

         The result is as follows:

 

Guess you like

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