如何在 SQL 中使用 UNION
一、SQL UNION 语法
UNION 是一个 SQL 运算符,它结合两个或多个 SELECT 查询的结果并在输出中提供单个集合。
SQL UNION 语法:
SELECT Column_Name_1, Column_Name_2 …., Column_NameN FROM Table_Name_1
UNION
SELECT Column_Name1, Column_Name_2 …., Column_Name_N FROM Table_Name_2
UNION ……. UNION
SELECT Column_Name1, Column_Name_2 …., Column_Name_N FROM Table_Name_N ;
与 UNION 运算符连接的每个 SELECT 语句的数据类型和字段数必须相同。数据库系统使用 UNION 运算符从组合结果集中删除重复值。
二、SQL UNION 使用步骤
如果要在SQL语言中使用 UNION 运算符,则必须创建两个不同的表并在两个表中添加多个记录。
以下查询创建具有四个字段的 Old_Worker 表:
CREATE TABLE Old_Worker
(
Worker_Id INT NOT NULL,
Worker_Name Varchar (40),
Worker_Age INT,
Worker_Salary INT
);
以下查询创建包含四个字段的 New_Worker 表:
CREATE TABLE New_Worker
(
Worker_Id INT NOT NULL,
Worker_Name Varchar (40),
Worker_Age INT,
Worker_Salary INT
);
下面的 INSERT 语句将 old Workers 的记录插入到 Old_Worker 表中:
INSERT INTO Old_Worker (Worker_Id, Worker_Name, Worker_Age, Worker_Salary) VALUES (101, Akhil, 28, 25000),
(102, Abhay, 27, 26000),
(103, Sorya, 26, 29000),
(104, Abhishek, 27, 26000),
(105, Ritik, 26, 29000),
(106, Yash, 29, 28000);
以下查询显示了 Old_Worker 表的详细信息:
SELECT * FROM Old_Worker;
输出结果为:
Worker_Id | Worker_Name | Worker_Age | Worker_Salary |
---|---|---|---|
101 | Akhil | 28 | 25000 |
102 | Abhay | 27 | 26000 |
103 | Sorya | 26 | 29000 |
104 | Abhishek | 27 | 26000 |
105 | Ritik | 26 | 29000 |
106 | Yash | 29 | 28000 |
表:Old_Worker
以下 INSERT 语句将新 Workers 的记录插入到 New_Worker 表中:
INSERT INTO New_Worker (Worker_Id, Worker_Name, Worker_Age, Worker_Salary) VALUES (201, Jack, 28, 45000),
(202, Berry, 29, 35000),
(105, Ritik, 26, 29000),
(203, Shyam, 27, 26000),
(204, Ritika, 28, 38000),
(106, Yash, 29, 28000);
以下查询显示了 New_Worker 表的详细信息:
SELECT * FROM New_Worker;
输出结果为:
Worker_Id | Worker_Name | Worker_Age | Worker_Salary |
---|---|---|---|
201 | Jack | 28 | 45000 |
202 | Berry | 29 | 35000 |
105 | Ritik | 26 | 29000 |
203 | Shyam | 27 | 26000 |
204 | Ritika | 28 | 38000 |
106 | Yash | 29 | 28000 |
表:New_Worker
以下查询使用 UNION 运算符在一个表中显示两个表的所有记录:
SELECT * FROM Old_Worker UNION SELECT * FROM New_Worker;
输出结果为:
Worker_Id | Worker_Name | Worker_Age | Worker_Salary |
---|---|---|---|
101 | Akhil | 28 | 25000 |
102 | Abhay | 27 | 26000 |
103 | Sorya | 26 | 29000 |
104 | Abhishek | 27 | 26000 |
105 | Ritik | 26 | 29000 |
106 | Yash | 29 | 28000 |
201 | Jack | 28 | 45000 |
202 | Berry | 29 | 35000 |
203 | Shyam | 27 | 26000 |
204 | Ritika | 28 | 38000 |
三、带有 UNION 运算符的 Where 语句
我们还可以将 WHERE 子句与 UNION SQL 运算符一起使用,这有助于从一个或多个表中选择特定记录。
带有 WHERE 子句的 UNION 语法:
SELECT Column_Name_1, Column_Name_2 …., Column_NameN FROM Table_Name_1 [WHERE condition]
UNION
SELECT Column_Name1, Column_Name_2 …., Column_Name_N FROM Table_Name_2 [WHERE condition];
四、带有 WHERE 子句的 UNION 示例
以下查询显示了上表中薪水大于等于 29000 的工人记录:
SELECT * FROM Old_Worker WHERE Worker_Salary >= 29000 UNION SELECT * FROM New_Worker WHERE Worker_Salary >= 29000;
输出结果为:
Worker_Id | Worker_Name | Worker_Age | Worker_Salary |
---|---|---|---|
103 Sorya | 26 | 29000 | |
105 Ritik | 26 | 29000 | |
201 Jack | 28 | 45000 | |
202 Berry | 29 | 35000 | |
204 | Ritika | 28 | 38000 |
五、SQL中的UNION ALL 运算符
SQL Union ALL 运算符与 UNION 运算符相同,但唯一的区别是 UNION ALL 运算符还显示结果中的公共行。
UNION ALL 集合运算符的语法:
SELECT Column_Name_1, Column_Name_2 …., Column_Name_N FROM Table_Name_1 [WHERE condition]
UNION ALL
SELECT Column_Name_1, Column_Name_2 …., Column_Name_N FROM Table_Name_2 [WHERE condition];
UNION ALL 示例
让我们以上述两个表为例,对这两个表执行 UNION ALL 运算符。
以下查询使用 UNION ALL 运算符在一个表中显示两个表中的所有唯一和公共记录:
SELECT * FROM Old_Worker UNION ALL SELECT * FROM New_Worker;
输出结果为:
Worker_Id | Worker_Name | Worker_Age | Worker_Salary |
---|---|---|---|
101 | Akhil | 28 | 25000 |
102 | Abhay | 27 | 26000 |
103 | Sorya | 26 | 29000 |
104 | Abhishek | 27 | 26000 |
105 | Ritik | 26 | 29000 |
106 | Yash | 29 | 28000 |
201 | Jack | 28 | 45000 |
202 | Berry | 29 | 35000 |
105 | Ritik | 26 | 29000 |
203 | Shyam | 27 | 26000 |
204 | Ritika | 28 | 38000 |
106 | Yash | 29 | 28000 |
热门文章
优秀文章