introduce:
In sql union
is an operator used to combine the result sets of two or more select statements. It combines the results of multiple queries into a single result set and automatically removes duplicate rows. Note that union
the operation requires that the queries being combined return the same number and type of columns.
usage:
union
The basic syntax of is as follows:
select_statement1
union
select_statement2;
Among them, select_statement1
and select_statement2
are two or more select statements whose results will be combined.
Example:
Suppose we have two tables, table1
and table2
, which have the same structure and both have name
and age
columns. We can union
merge their data using:
-- 从 table1 和 table2 表中选择 name 和 age 列,并合并结果集
select name, age from table1
union
select name, age from table2;
Note: If you want to keep all rows, including duplicates, you can use union all
instead of union
. union all
You can read the introduction of the next article~
hair