前提:建立兩個關係表:java
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) );
表數據以下:mysql
一、獲取A、B公有部分sql
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++"數據都被過濾ide
二、A的獨有及AB共有3d
使用左外連獲取A全部的數據,篩選出B表中符合on表達式的數據,不符合的數據B表相應字段補NULLblog
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。it
三、B獨有及AB共有io
同上,使用右外連,過濾掉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"數據
四、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
五、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
六、求並集
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
七、求差集
所謂差集就是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