📅  最后修改于: 2023-12-03 15:05:18.005000             🧑  作者: Mango
SQL UNION is a clause used in SQL queries to combine the results of two or more SELECT statements into a single result set. It is similar to the JOIN clause, but instead of joining tables, it joins the results of the SELECT statements.
The syntax for the SQL UNION statement is as follows:
SELECT column1, column2, ... FROM table1
UNION
SELECT column1, column2, ... FROM table2;
In this example, two SELECT statements are combined using the UNION clause. The result set will contain all the rows from both SELECT statements. It is important to note that the columns in each SELECT statement must be in the same order and have the same data types.
The SQL UNION clause also has the following requirements:
Here is an example of a SQL UNION query:
SELECT employee_id, first_name, last_name FROM employees
UNION
SELECT customer_id, first_name, last_name FROM customers;
This query combines the employee and customer tables into a single result set, containing the employee and customer names.
In conclusion, the SQL UNION clause is a useful tool for combining the results of multiple SQL queries into a single result set. It is important to keep in mind the requirements and limitations of the clause when using it in your SQL queries.
-- Here is an example of a SQL UNION query:
SELECT employee_id, first_name, last_name FROM employees
UNION
SELECT customer_id, first_name, last_name FROM customers;