The UNION operator in SQL is used to merge data in tables. Its characteristics are: merging tables, requiring column matching, and retaining duplicate rows. Uses include: merging complementary data, finding duplicate data, creating summary reports, and deduplicating data. Example: SELECT customer_id, first_name, last_name FROM customers UNION SELECT customer_id, first_name, last_name FROM orders; will merge customer data from the customers and orders tables, including duplicate rows.
UNION in SQL
UNION is an operator in SQL that is used to Merge data from two or more tables. It appends the rows from the input table together to create a new results table.
Syntax
<code class="sql">SELECT 列名1, 列名2, ... FROM 表1 UNION SELECT 列名1, 列名2, ... FROM 表2;</code>
Features
Usage
UNION is mainly used in the following situations:
Example
The following example merges customer data from two tables named customers
and orders
For a result table:
<code class="sql">SELECT customer_id, first_name, last_name FROM customers UNION SELECT customer_id, first_name, last_name FROM orders;</code>
The result table will contain customer information from both tables, including duplicate rows.
The above is the detailed content of What does union in sql mean?. For more information, please follow other related articles on the PHP Chinese website!