6.4.1.2 UNION Syntax
Kod
SELECT ...
UNION [ALL]
SELECT ...
[UNION
SELECT ...]
UNION is implemented in MySQL 4.0.0.
UNION is used to combine the result from many SELECT statements into one result set.
The columns listed in the select_expression portion of the SELECT should have the same type. The column names used in the first SELECT query will be used as the column names for the results returned.
The SELECT commands are normal select commands, but with the following restrictions:
:arrow:
Only the last SELECT command can have INTO OUTFILE.
If you don't use the keyword ALL for the UNION, all returned rows will be unique, as if you had done a DISTINCT for the total result set. If you specify ALL, then you will get all matching rows from all the used SELECT statements.
If you want to use an ORDER BY for the total UNION result, you should use parentheses:
Kod
(SELECT a FROM table_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM table_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10)
ORDER BY a;