sql server union
时间: 2023-09-27 18:04:26 浏览: 141
The UNION operator in SQL Server is used to combine the result sets of two or more SELECT statements into a single result set.
The syntax for using the UNION operator is as follows:
SELECT column1, column2, ...
FROM table1
WHERE condition(s)
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition(s);
The columns in the SELECT statements must have the same data type and be in the same order. The WHERE clause is optional, and the results of each SELECT statement are included in the final result set.
The UNION operator removes any duplicate rows from the result set. If you want to include all rows, including duplicates, you can use the UNION ALL operator instead.
Example:
SELECT customer_id, customer_name
FROM customers
WHERE customer_country = 'USA'
UNION
SELECT customer_id, customer_name
FROM customers
WHERE customer_country = 'Canada';
This query will return a single result set that includes all customers from the USA and Canada, with no duplicates.
阅读全文