The original text is reproduced from: http://www.cnblogs.com/kissdodog/archive/2013/06/24/3152743.html
The following is the reproduced content:
In order to cooperate with the test, two tables were specially built, and some test data were added, in which the characters of Soochow were repeatedly recorded.
Table: Person_1 Wei State Character
Table: Person_2 Shu State Character
A, Union Formation Union
Union can join two or more result sets to form a "union". All records in the subresult set are grouped together to form a new result set.
1. Restrictions:
If you use Union to join the result set, there are 4 qualifications.
(1) The sub-result sets must have the same structure.
(2) The number of columns in the word result set must be the same.
(3) The data types corresponding to the sub-result sets must be compatible.
(4) Each sub-result set cannot contain order by and compute clauses.
2. Grammatical form
select_statement union [all] select_statement
all means that the final result set will contain all the rows, and duplicate rows cannot be deleted.
3. Example:
SELECT Name FROM Person_1 UNION SELECT Name FROM Person_2
The result generated is:
Noticing the duplicate records, Sun Quan and Zhou Yu only showed one. Let's replace UNION with UNION ALL to see what the result is:
SELECT Name FROM Person_1 UNION ALL SELECT Name FROM Person_2
Noticed the duplicate records, Sun Quan and Zhou Yu appeared twice, this is the difference between UNION ALL and UNION.
B. Except forms a difference set
Except can join two or more result sets to form a "difference set". Returns records that are already in the result set on the left, but not in the result set on the right.
1. Restrictions:
(1) The sub-result sets must have the same structure.
(2) The number of columns in the sub-result set must be the same.
(3) The data types corresponding to the sub-result sets must be compatible.
(4) Each sub-result set cannot contain order by and compute clauses.
2. Grammatical form:
select_statement except select_statement
Automatically remove duplicate rows.
3. Example:
SELECT Name FROM Person_1 EXCEPT SELECT Name FROM Person_2
Result:
Note that there is a list of Person_2, Sun Quan and Zhou Yu have been removed.
C. InterSect forms an intersection
InterSect can join two or more result sets to form an "intersection". Returns the records in both the left result set and the right result set.
1. Restrictions:
If you use Except to connect result sets, there are 4 qualifications.
(1) The sub-result sets must have the same structure.
(2) The number of columns in the sub-result set must be the same.
(3) The data types corresponding to the sub-result sets must be compatible.
(4) Each sub-result set cannot contain order by or compute clauses.
2. Grammatical form:
select_statement intersect select_statement
SELECT Name FROM Person_1 INTERSECT SELECT Name FROM Person_2The results returned are as follows:
Note that only records that have both tables (Zhou Yu, Sun Quan) are taken, this is the so-called intersection.
D. Sorting the result set
SELECT Name FROM Person_1 INTERSECT SELECT Name FROM Person_2 ORDER BY Name DESC -- the field names here are the same, if they are different, please remember to sort the column name, it can only be the column name of the first table
There are only two things to note here:
1. ORDER BY sorts the results of the entire operation, not a single data set.
2. The field name sorted after ORDER BY is the field name or alias of the first dataset.