- MySQL 5.1 Reference Manual :: 12 SQL Statement Syntax :: 12.2 Data Manipulation Statements :: 12.2.8 SELECT Syntax :: 12.2.8.3 UNION Syntax
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
SELECT ... UNION [ALL | DISTINCT] SELECT ... [UNION [ALL | DISTINCT] SELECT ...]
UNION
is used to combine the result from multipleSELECT
statements into a single result set.The column names from the first
SELECT
statement are used as the column names for the results returned. Selected columns listed in corresponding positions of eachSELECT
statement should have the same data type. (For example, the first column selected by the first statement should have the same type as the first column selected by the other statements.)If the data types of corresponding
SELECT
columns do not match, the types and lengths of the columns in theUNION
result take into account the values retrieved by all of theSELECT
statements. For example, consider the following:mysql>
SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+---------------+ | REPEAT('a',1) | +---------------+ | a | | bbbbbbbbbb | +---------------+(In some earlier versions of MySQL, only the type and length from the first
SELECT
would have been used and the second row would have been truncated to a length of 1.)The
SELECT
statements are normal select statements, but with the following restrictions:Only the last
SELECT
statement can useINTO OUTFILE
. (However, the entireUNION
result is written to the file.)HIGH_PRIORITY
cannot be used withSELECT
statements that are part of aUNION
. If you specify it for the firstSELECT
, it has no effect. If you specify it for any subsequentSELECT
statements, a syntax error results.
The default behavior for
UNION
is that duplicate rows are removed from the result. The optionalDISTINCT
keyword has no effect other than the default because it also specifies duplicate-row removal. With the optionalALL
keyword, duplicate-row removal does not occur and the result includes all matching rows from all theSELECT
statements.You can mix
UNION ALL
andUNION DISTINCT
in the same query. MixedUNION
types are treated such that aDISTINCT
union overrides anyALL
union to its left. ADISTINCT
union can be produced explicitly by usingUNION DISTINCT
or implicitly by usingUNION
with no followingDISTINCT
orALL
keyword.To use an
ORDER BY
orLIMIT
clause to sort or limit the entireUNION
result, parenthesize the individualSELECT
statements and place theORDER BY
orLIMIT
after the last one. The following example uses both clauses:(SELECT a FROM t1 WHERE a=10 AND B=1) UNION (SELECT a FROM t2 WHERE a=11 AND B=2) ORDER BY a LIMIT 10;
This kind of
ORDER BY
cannot use column references that include a table name (that is, names intbl_name
.col_name
format). Instead, provide a column alias in the firstSELECT
statement and refer to the alias in theORDER BY
. (Alternatively, refer to the column in theORDER BY
using its column position. However, use of column positions is deprecated.)Also, if a column to be sorted is aliased, the
ORDER BY
clause must refer to the alias, not the column name. The first of the following statements will work, but the second will fail with anUnknown column 'a' in 'order clause'
error:(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b; (SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;
To apply
ORDER BY
orLIMIT
to an individualSELECT
, place the clause inside the parentheses that enclose theSELECT
:(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10) UNION (SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
However, use of
ORDER BY
for individualSELECT
statements implies nothing about the order in which the rows appear in the final result becauseUNION
by default produces an unordered set of rows. Therefore, the use ofORDER BY
in this context is typically in conjunction withLIMIT
, so that it is used to determine the subset of the selected rows to retrieve for theSELECT
, even though it does not necessarily affect the order of those rows in the finalUNION
result. IfORDER BY
appears withoutLIMIT
in aSELECT
, it is optimized away because it will have no effect anyway.To cause rows in a
UNION
result to consist of the sets of rows retrieved by eachSELECT
one after the other, select an additional column in eachSELECT
to use as a sort column and add anORDER BY
following the lastSELECT
:(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1) UNION (SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
To additionally maintain sort order within individual
SELECT
results, add a secondary column to theORDER BY
clause:(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1) UNION (SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col, col1a;
Use of an additional column also enables you to determine which
SELECT
each row comes from. Extra columns can provide other identifying information as well, such as a string that indicates a table name.