Previously did not know the Union and Union All in the end what is the difference, look good today, found on the Internet result looks like this, but still not very understanding, will personally verify yourself:
Union: the results of the two sets and set operations, not including duplicates, while default sorting rule;
Union All: two result sets and set operations, including duplicates, do not sort;
Here's a simple test (because it is a test, so sql code is written in a very simple, nothing very strictly regulated, just to understand the difference between the two)
Strict standard wording should first determine the database exists, the table exists and so constrained
The first step, building a database:
- Create database Test
- go
- use Test
- go
The second step, to build the table:
- Create table Table1
- (
- id int not null,
- name varchar(20) not null
- )
- Create table Table2
- (
- id int not null,
- name varchar(20) not null
- )
A third step of inserting the test data:
- Insert into Table1 values (1,'姚羽')
- Insert into Table1 values (2, 'side Bingbing')
- Insert into Table1 values (3,'袁磊')
- Insert into Table2 values (1,'姚羽')
- Insert into Table2 values (2,'柳春平')
- Insert into Table2 values (3,'张永超')
- Insert into Table2 values (4,'刘华健')
The fourth step, the test begins:
- select * from Table1
- select * from Table2
Query results following the implementation of two tables
Can easily be seen, is inserted among the above test data, there is a duplicate
Then we look at the implementation of union look
- select * from Table1
- union
- select * from Table2
Then perform union all to see
- select * from Table1
- union all
- select * from Table2