UnionAn operator combines two or more query results into a single result set.
This result set contains all rows of all queries in the Union query. This is different from using join to combine columns in two tables.
UseUnionThe result set of a combined query has two basic rules:
1. The columns and columns in all queries must be in the same order.
2. Data type must be compatible
. union name of the result set column and the first select the column names in the result set in the statement are the same, others select the column name of the result set in the statement is ignored.
B. by default, Union the operator deletes duplicate rows from the result set. If all keyword, the result set contains all rows without deleting duplicate rows.
C. SQLIs left to right to includeUnionThe statement of the operator. You can use parentheses to change the order of evaluation.
--For example:
Select * From tablea
Union all
(
Select * From tableb
Union all
Select * From tablec
)
In this way, you can firstTablebAndTablecMerge and then mergeTablea
D. to save the merged result set to a new data table, then into the statement must be added to the first select medium
E. only the last select used in the statement order by and compute clause, which affects the sorting and counting summary of the final merge results
F. GroupAndHavingThe sub-statement can beSelectUsed in the query, they do not affect the final result
-- for example, :
select name as name , class as class , grade as Grade
Into # students
From stud87
Union all
Select * From stud88
Union all
Select * From stud89
OrderGrade
Set3Merge results of classes (GradeSort) insert to temporary table# StudentsInside
I hope the Knowledge mentioned above will prompt you.
Of course, you are welcome to contact us and correct us.
Author: aierong
Blog: http://www.cnblogs.com/aierong
Email: aierong@126.com