赞
踩
假如我们有两张表,第一个表名为 students,如下所示:
+------------+---------+----------+
| student_id | name | class_id |
+------------+---------+----------+
| 1 | Alice | 101 |
| 2 | Bob | 102 |
| 3 | Charlie | 101 |
| 4 | David | 103 |
| 5 | Eve | 102 |
+------------+---------+----------+
第二个表名为 classes,如下所示:
+----------+------------+
| class_id | class_name |
+----------+------------+
| 101 | Math |
| 102 | Science |
| 103 | History |
+----------+------------+
我们创建第三张表,名为 students_2,如下所示:
+------------+-------+----------+
| student_id | name | class_id |
+------------+-------+----------+
| 2 | Bob | 102 |
| 7 | Grace | 105 |
| 8 | Henry | 102 |
+------------+-------+----------+
接下来,介绍几种常见的集合运算:
UNION(并集): 返回两个表的所有唯一行。重复的行会被去除。
SELECT * FROM students UNION SELECT * FROM students_2; +------------+---------+----------+ | student_id | name | class_id | +------------+---------+----------+ | 1 | Alice | 101 | | 2 | Bob | 102 | | 3 | Charlie | 101 | | 4 | David | 103 | | 5 | Eve | 102 | | 7 | Grace | 105 | | 8 | Henry | 102 | +------------+---------+----------+ 7 rows in set (0.00 sec)
在使用
UNION
关键字时,必须确保两个表具有相同数量的列。这是因为UNION
操作是通过将一个表的行添加到另一个表的行来工作的,所以两个表的列数必须相同。如果列数不匹配,查询将无法执行,并会返回错误。
当我们在UNION
关键字后添加ALL
时,两个表中的所有数据都将被包含在内。这意味着即使两个表中存在重复的数据,它们也会被保留。这是因为UNION ALL
不会去除结果中的重复行。所以,如果你希望在结果中保留所有数据,包括重复的数据,那么就应该使用UNION ALL
。
SELECT * FROM students UNION ALL SELECT * FROM students_2; +------------+---------+----------+ | student_id | name | class_id | +------------+---------+----------+ | 1 | Alice | 101 | | 2 | Bob | 102 | | 3 | Charlie | 101 | | 4 | David | 103 | | 5 | Eve | 102 | | 2 | Bob | 102 | | 7 | Grace | 105 | | 8 | Henry | 102 | +------------+---------+----------+
此时重复行已经被保留了。
INTERSECT(交集): 返回同时出现在两个表中的行。
SELECT * FROM students
INTERSECT
SELECT * FROM students_2;
+------------+---------+----------+
| student_id | name | class_id |
+------------+---------+----------+
| 2 | Bob | 102 |
+------------+---------+----------+
EXCEPT(差集): 返回出现在第一个表中但不出现在第二个表中的行。
SELECT * FROM students
EXCEPT
SELECT * FROM students_2;
+------------+---------+----------+
| student_id | name | class_id |
+------------+---------+----------+
| 1 | Alice | 101 |
| 3 | Charlie | 101 |
| 4 | David | 103 |
| 5 | Eve | 102 |
+------------+---------+----------+
联结(JOIN)是 SQL 中一种用于合并两个或多个表格中数据的操作。它允许根据某些相关列的值将多个表格中的行组合在一起,从而创建一个包含了来自不同表的相关数据的结果集。
以下是一些常见类型的SQL联结:
下面就对常用的进行一下介绍:
内联结(INNER JOIN):
SELECT students.name, classes.class_name
FROM students
INNER JOIN classes ON students.class_id = classes.class_id;
+---------+------------+
| name | class_name |
+---------+------------+
| Alice | Math |
| Bob | Science |
| Charlie | Math |
| David | History |
| Eve | Science |
+---------+------------+
5 rows in set (0.00 sec)
关键字
ON
一定不能省略!!!其中
SELECT
后面的是<表名>.<列名>
或<表的别名>.<列名>
。
WHERE
,GROUP BY
,HAVING
等工具都还可以正常的使用
左联结(LEFT JOIN):
SELECT students.name, classes.class_name
FROM students
LEFT JOIN classes ON students.class_id = classes.class_id;
+---------+------------+
| name | class_name |
+---------+------------+
| Alice | Math |
| Bob | Science |
| Charlie | Math |
| David | History |
| Eve | Science |
+---------+------------+
5 rows in set (0.00 sec)
右联结(RIGHT JOIN):
SELECT students.name, classes.class_name
FROM students
RIGHT JOIN classes ON students.class_id = classes.class_id;
+---------+------------+
| name | class_name |
+---------+------------+
| Alice | Math |
| Bob | Science |
| Charlie | Math |
| David | History |
| Eve | Science |
+---------+------------+
5 rows in set (0.00 sec)
交叉联结(CROSS JOIN):
SELECT students.name, classes.class_name FROM students CROSS JOIN classes; +---------+------------+ | name | class_name | +---------+------------+ | Alice | Math | | Alice | Science | | Alice | History | | Bob | Math | | Bob | Science | | Bob | History | | Charlie | Math | | Charlie | Science | | Charlie | History | | David | Math | | David | Science | | David | History | | Eve | Math | | Eve | Science | | Eve | History | +---------+------------+ 15 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。