SQL_STUDY: 8.SQL UNION operator UNION ALL and

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/NumberOneStudent/article/details/102720142

Summary:

  1. UNION merge two queries
  2. UNION ALL queries can be sorted

SQL UNION operator UNION ALL and

SQL UNION operator

UNION operation result set operator for combining two or more of the SELECT statement.
Please note that the interior of the UNION SELECT statement must have the same number of columns. The columns must also have similar data types. Meanwhile, the order of columns in each SELECT statement must be the same.

SQL UNION Syntax

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

Note: By default, UNION operator to select a different value. If you allow duplicate values, use UNION ALL.

SQL UNION ALL syntax

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

In addition, UNION result set is always equal to the column names in the first column name UNION SELECT statement.

The following example uses the original table:
Employees_China:
e_id e_name
01 Zhang, Hua
02 Wang, Wei
03 Carter, Thomas
04 Yang, Ming

Employees_USA:

E_ID E_Name
01 Adams, John
02 Bush, George
03 Carter, Thomas
04 Gates, Bill


UNION ALL

UNION UNION ALL command and the command is almost equivalent, but UNION ALL command lists all values.

UNION ALL can be sorted

SELECT column_name(s) FROM table_name1 order by column_name
UNION ALL
SELECT column_name(s) FROM table_name2  order by column_name

Will be put in front of table_name1 check out
table_name2 sort on the back

UNION sort is useless

Guess you like

Origin blog.csdn.net/NumberOneStudent/article/details/102720142