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操作符的示例
让我们创建两个不同的表并在两个表中插入记录。
以下查询用于创建 Old_Employee 表,该表包含四个字段:
CREATE TABLE Old_Employee
(
Employee_Id INT NOT NULL,
Employee_Name Varchar (40),
Emp_Age INT,
Emp_Salary INT
);
下面的查询创建了一个名为 New_Employee 的表,该表有四个字段:
CREATE TABLE New_Employee
(
Employee_Id INT NOT NULL,
Employee_Name Varchar (40),
Emp_Age INT,
Emp_Salary INT
);
下面的INSERT查询将旧员工的记录插入到Old_Employee表中:
INSERT INTO Old_Employee (Employee_Id, Employee_Name, Emp_Age, Emp_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_Employee 表的详细信息:
SELECT * FROM Old_Employee;
Employee_Id | Employee_Name | Emp_Age | Emp_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_Employee
以下INSERT查询将新员工的记录插入 New_Employee 表:
INSERT INTO New_Employee (Employee_Id, Employee_Name, Emp_Age, Emp_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_Employee 表的详细信息:
SELECT * FROM New_Employee;
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
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_Employee
以下查询使用 UNION 运算符将两个表格的所有记录显示在一个表格中:
SELECT * FROM Old_EmployeeUNION SELECT * FROM New_Employee;
输出:
Employee_Id | Employee_Name | Emp_Age | Emp_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操作符一起使用,以过滤来自一个或两个表的记录。
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];
UNION与WHERE子句的示例
以下查询显示了以上表中薪水大于等于29000的员工记录:
SELECT * FROM Old_Employee WHERE Emp_Salary >= 29000UNION SELECT * FROM New_Employee WHERE Emp_Salary >= 29000;
输出:
Employee_Id | Employee_Name | Emp_Age | Emp_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示例
让我们创建两个不同的表并在两个表中插入记录。
以下查询创建了 Passed_Students 表,包含四个字段:
CREATE TABLE Passed_Students
(
Student_Id INT NOT NULL,
Student_Name Varchar (40),
Student_Age INT,
Student_Marks INT
);
下面的查询创建了 New_Students 数据表,包含四个字段:
CREATE TABLE New_Students
(
Student_Id INT NOT NULL,
Student_Name Varchar (40),
Student_Age INT,
Student_Marks INT
);
下面的INSERT查询将通过学生的记录插入到Passed_Students表中:
INSERT INTO Passed_Students (Student_Id, Student_Name, Student_Age, Student_Marks) VALUES (101, Akhil, 28, 95),
(102, Abhay, 27, 86),
(103, Sorya, 26, 79),
(104, Abhishek, 27, 66),
(105, Ritik, 26, 79),
(106, Yash, 29, 88);
以下查询显示了Passed_Students表的详细信息:
SELECT * FROM Passed_Students;
Student_Id | Student_Name | Student_Age | Student_Marks |
---|---|---|---|
101 | Akhil | 28 | 95 |
102 | Abhay | 27 | 86 |
103 | Sorya | 26 | 79 |
104 | Abhishek | 27 | 66 |
105 | Ritik | 26 | 79 |
106 | Yash | 29 | 88 |
表:Passed_Students
以下INSERT查询将新学生的记录插入到New_Students表中:
INSERT INTO New_Students (Student_Id, Student_Name, Student_Age, Student_Marks) VALUES (201, Jack, 28, 77),
(202, Berry, 29, 68),
(105, Ritik, 26, 82),
(203, Shyam, 27, 70),
(204, Ritika, 28, 99),
(106, Yash, 29, 86);
以下查询显示了 New_Students 表的详细信息:
SELECT * FROM New_Students;
Student_Id | Student_Name | Student_Age | Student_Marks |
---|---|---|---|
201 | Jack | 28 | 77 |
202 | Berry | 29 | 66 |
105 | Ritik | 26 | 82 |
203 | Shyam | 27 | 70 |
204 | Ritika | 28 | 99 |
106 | Yash | 29 | 86 |
表格:New_Students
以下查询显示了两个表中所有重复和唯一的记录:
SELECT * FROM Passed_StudentsUNION ALL SELECT * FROM New_Students;
输出:
Student_Id | Student_Name | Student_Age | Student_Marks |
---|---|---|---|
101 | Akhil | 28 | 95 |
102 | Abhay | 27 | 86 |
103 | Sorya | 26 | 79 |
104 | Abhishek | 27 | 66 |
105 | Ritik | 26 | 79 |
106 | Yash | 29 | 88 |
201 | Jack | 28 | 77 |
202 | Berry | 29 | 68 |
105 | Ritik | 26 | 82 |
203 | Shyam | 27 | 70 |
204 | Ritika | 28 | 99 |
106 | Yash | 29 | 86 |