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
如果要在结构化查询语言中使用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_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查询将新工人的记录插入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 |
WHERE子句与UNION运算符
我们还可以在UNION SQL运算符中使用WHERE子句,以便从一个或多个表中选择特定记录。
UNION与WHERE子句的语法
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];
联合查询与范围限定条件示例
下面的查询显示了上述表格中薪水大于等于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 Set运算符的语法:
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 |