【注意】:Oracle數據庫支持full join,mysql是不支持full join的,但仍然能夠同過左外鏈接+ union+右外鏈接實現mysql
初始化SQL語句:sql
-
-
drop database if exists test;
-
-
-
-
-
-
create table t1 (id int not null,name varchar(20));
-
insert into t1 values (1,'t1a');
-
insert into t1 values (2,'t1b');
-
insert into t1 values (3,'t1c');
-
insert into t1 values (4,'t1d');
-
insert into t1 values (5,'t1f');
-
-
-
-
create table t2 (id int not null,name varchar(20));
-
insert into t2 values (2,'t2b');
-
insert into t2 values (3,'t2c');
-
insert into t2 values (4,'t2d');
-
insert into t2 values (5,'t2f');
-
insert into t2 values (6,'t2a');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
一、笛卡爾積
兩表關聯,把左表的列和右表的列經過笛卡爾積的形式表達出來。數據庫
mysql> select * from t1 join t2;
二、左鏈接
兩表關聯,左表所有保留,右表關聯不上用null表示。oracle
mysql> select * from t1 left join t2 on t1.id = t2.id;
三、右鏈接
右表所有保留,左表關聯不上的用null表示。app
mysql> select * from t1 right join t2 on t1.id =t2.id;
四、內鏈接
兩表關聯,保留兩表中交集的記錄。ui
mysql> select * from t1 inner join t2 on t1.id = t2.id;
五、左表獨有
兩表關聯,查詢左表獨有的數據。spa
mysql> select * from t1 left join t2 on t1.id = t2.id where t2.id is null;
六、右表獨有
兩表關聯,查詢右表獨有的數據。3d
mysql> select * from t1 right join t2 on t1.id = t2.id where t1.id is null;
七、全鏈接
兩表關聯,查詢它們的全部記錄。code
oracle裏面有full join,可是在mysql中沒有full join。咱們能夠使用union來達到目的。blog
-
mysql>
select * from t1 left join t2 on t1.id = t2.id
-
-
->
select * from t1 right join t2 on t1.id = t2.id;
八、並集去交集
兩表關聯,取並集而後去交集。
-
mysql>
select * from t1 left join t2 on t1.id = t2.id where t2.id is null
-
-
->
select * from t1 right join t2 on t1.id = t2.id where t1.id is null;