The UNION operator combines the results of multiple SELECT statements into a result set that does not contain duplicate rows. Its characteristics include: the column order and data type of the merged result set must be the same. Aggregate functions are not supported. UNION ALL can be used to combine result sets that contain duplicate rows to find duplicate data.
Usage of UNION in SQL
The UNION operator is used in SQL to combine two or more SELECT The results of the statements are combined into a result set.
Usage:
SELECT statement UNION SELECT statement UNION...
Features:
Example:
Suppose we have two tables:
<code>表 Student: | id | name | | --- | ---- | | 1 | John | | 2 | Mary | 表 Teacher: | id | name | | --- | ---- | | 3 | David | | 4 | Lisa |</code>
We can use the UNION operator to merge the # of these two tables ##name Column:
<code class="sql">SELECT name FROM Student UNION SELECT name FROM Teacher;</code>
<code>| name | | --- | | John | | Mary | | David | | Lisa |</code>
Note:
The above is the detailed content of How to use union in sql. For more information, please follow other related articles on the PHP Chinese website!