The UNION operator combines rows from multiple tables with the same column structure into a single result set, eliminating duplicate rows and automatically converting data types. 1. Merge rows from different tables; 2. Eliminate duplicate rows; 3. Convert data types to match column structure.
The role of UNION in SQL
The UNION operator is used to merge data from two or more tables with Rows with the same column structure form a new result set.
Usage:
The syntax of UNION is as follows:
<code>SELECT 列名1, 列名2, ... FROM 表名1 UNION SELECT 列名1, 列名2, ... FROM 表名2</code>
Function:
Notes:
Example:
Merge rows from two tables containing country information:
<code>SELECT Country, Population FROM Countries UNION SELECT Country, Population FROM World_Nations;</code>
Result:
The merged table will contain data for all countries with no duplicate rows.
The above is the detailed content of The role of union in sql. For more information, please follow other related articles on the PHP Chinese website!