前提:创建两个关系表:
CREATE TABLE t_blog( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(50), typeId INT);CREATE TABLE t_type( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20));
表数据如下:
1、获取A、B公有部分
MySql提供一种内连接的方式来获取A、B两表中均符合on表达式的数据:
mysql> select * from t_blog b inner join t_type t on b.typeId = t.id;+----+-------------------+--------+----+-------+| id | title | typeId | id | name |+----+-------------------+--------+----+-------+| 1 | java基础类型 | 1 | 1 | JAVA || 2 | java编程思想 | 1 | 1 | JAVA || 3 | java从入门到放弃 | 1 | 1 | JAVA || 4 | C语言精讲 | 2 | 2 | C || 5 | C语言从入门到放弃 | 2 | 2 | C || 6 | mysql基础 | 4 | 4 | MYSQL |+----+-------------------+--------+----+-------+6 rows in set
通过内连接,blog中的"图解http"数据和type表中的"C++"数据都被过滤
2、A的独有及AB共有
使用左外连获取A所有的数据,筛选出B表中符合on表达式的数据,不符合的数据B表相应字段补NULL
mysql> select * from t_blog b left join t_type t on b.typeId = t.id;+----+-------------------+--------+------+-------+| id | title | typeId | id | name |+----+-------------------+--------+------+-------+| 1 | java基础类型 | 1 | 1 | JAVA || 2 | java编程思想 | 1 | 1 | JAVA || 3 | java从入门到放弃 | 1 | 1 | JAVA || 4 | C语言精讲 | 2 | 2 | C || 5 | C语言从入门到放弃 | 2 | 2 | C || 6 | mysql基础 | 4 | 4 | MYSQL || 7 | 图解http | NULL | NULL | NULL |+----+-------------------+--------+------+-------+7 rows in set
通过左外连,左表中所有的数据都被查询出来,右表独有的"C++"数据被过滤,左表独有的"图解http"数据的右表位置补了NULL。
3、B独有及AB共有
同上,使用右外连,过滤掉A表中不符合on条件的数据,查询出所有B表中的数据,A表不满足的字段会补NULL;
mysql> select * from t_blog b right join t_type t on b.typeId = t.id;+------+-------------------+--------+----+-------+| id | title | typeId | id | name |+------+-------------------+--------+----+-------+| 1 | java基础类型 | 1 | 1 | JAVA || 2 | java编程思想 | 1 | 1 | JAVA || 3 | java从入门到放弃 | 1 | 1 | JAVA || 4 | C语言精讲 | 2 | 2 | C || 5 | C语言从入门到放弃 | 2 | 2 | C || NULL | NULL | NULL | 3 | C++ || 6 | mysql基础 | 4 | 4 | MYSQL |+------+-------------------+--------+----+-------+7 rows in set
通过右外连,右表中的数据全部被查出,独有的"C++"数据左表位置补了NULL,过滤掉左表的"图解http"数据
4、A独有
已知通过左外连,会得到A的独有及AB共有,在A的独有部分,B表位置会补NULL,也就是说,左外连结果中B为NULL的数据就是A的独有
mysql> select * from t_blog b left join t_type t on b.typeId = t.id where t.name is null;+----+----------+--------+------+------+| id | title | typeId | id | name |+----+----------+--------+------+------+| 7 | 图解http | NULL | NULL | NULL |+----+----------+--------+------+------+1 row in set
5、B独有
同上,右外连会获取B 的独有及AB共有,在B独有部分,A表位置会补NULL,也就是说左外连结果中A表为NULL的数据就是B的独有
mysql> select * from t_blog b right join t_type t on b.typeId = t.id where b.title is null;+------+-------+--------+----+------+| id | title | typeId | id | name |+------+-------+--------+----+------+| NULL | NULL | NULL | 3 | C++ |+------+-------+--------+----+------+1 row in set
6、求并集
MySql中提供"union"命令求并集,并且自动去重。如果对面表没有匹配,则补NULL
mysql> select * from t_blog b left join t_type t on b.typeId = t.id -> union -> select * from t_blog b right join t_type t on b.typeId = t.id;+------+-------------------+--------+------+-------+| id | title | typeId | id | name |+------+-------------------+--------+------+-------+| 1 | java基础类型 | 1 | 1 | JAVA || 2 | java编程思想 | 1 | 1 | JAVA || 3 | java从入门到放弃 | 1 | 1 | JAVA || 4 | C语言精讲 | 2 | 2 | C || 5 | C语言从入门到放弃 | 2 | 2 | C || 6 | mysql基础 | 4 | 4 | MYSQL || 7 | 图解http | NULL | NULL | NULL || NULL | NULL | NULL | 3 | C++ |+------+-------------------+--------+------+-------+8 rows in set
7、求差集
所谓差集就是A的独有和B 的独有的并集
mysql> select * from t_blog b left join t_type t on b.typeId = t.id where t.name is null -> union -> select * from t_blog b right join t_type t on b.typeId = t.id where b.title is null;+------+----------+--------+------+------+| id | title | typeId | id | name |+------+----------+--------+------+------+| 7 | 图解http | NULL | NULL | NULL || NULL | NULL | NULL | 3 | C++ |+------+----------+--------+------+------+2 rows in set