The UNION syntax is used to merge analysis results of multiple SELECT statements.
Syntax Format
* | SELECT [column name(KEY)] FROM table1 UNION SELECT [column name(KEY)] FROM table2
The analysis results of each SELECT statement should have the same number of columns and the same field types to be merged.
If the SELECT result contains duplicate rows, UNION retains only one instance of each by default. To retain all, use UNION ALL.
Syntax Example
Merge two tables that have only one column. Among them, the first table has only one row with the value 13, and the second table has two rows with values 42 and 13. Merge the duplicate rows.
* | SELECT * FROM (VALUES 13)
UNION
SELECT * FROM (VALUES 42,13)
Return result:
Merge two tables that have only one column. Among them, the first table has only one row with the value 13, and the second table has two rows with values 42 and 13. Do not merge the duplicate rows.
* | SELECT * FROM (VALUES 13)
UNION ALL
SELECT * FROM (VALUES 42,13)
Return result: