联合——union
# 联合——union
# union
The UNION operator is used to combine the result-set (only distinct values) of two or more SELECT statements.
提示
union 仅显示非重复值。
select 的字段的个数要一致。
Query: (To selects all the different cities (only distinct values) from the "Customers" and the "Suppliers" tables)
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
1
2
3
4
2
3
4
Result:
Number of Records: 10
City
------
Aachen
Albuquerque
Anchorage
Annecy
Barcelona
Barquisimeto
Bend
Bergamo
Berlin
Bern
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# union all
UNION ALL to select all (duplicate values also) cities from the "Customers" and "Suppliers" tables.
提示
union all 重复值也会显示。
Query:
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
1
2
3
4
2
3
4
Result:
Number of Records: 12
City
-------
Aachen
Aachen
Albuquerque
Anchorage
Annecy
Barcelona
Barquisimeto
Bend
Bend
Bergamo
Berlin
Berlin
Bern
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
编辑 (opens new window)
上次更新: 2022/09/26, 16:55:15