mysql UNION
时间: 2023-10-27 13:52:09 浏览: 83
The UNION operator is used in MySQL to combine the result set 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
UNION
SELECT column1, column2, ... FROM table2
UNION
SELECT column1, column2, ... FROM table3
...
The SELECT statements within the UNION must have the same number of columns and compatible data types. The result set of the UNION operator will contain only distinct rows, meaning that any duplicate rows will be eliminated.
In addition to the standard UNION operator, MySQL also provides two other variations:
- UNION ALL: This operator works the same as UNION, but it does not eliminate duplicate rows. This can be useful if you want to include all rows from all SELECT statements, including duplicates.
- UNION DISTINCT: This operator is the same as UNION, but it explicitly specifies that the result set should only contain distinct rows. This can be useful if you want to ensure that there are no duplicates in the result set, even if the SELECT statements themselves may contain duplicates.
阅读全文