之前学习mysql的时候对于老师说的左连接,右连接…之类的概念一直不清楚,模模糊糊的。工作之后理解这些名词一概念,现在把它记录一下。也希望能够帮助对于mysql中join不太清晰的朋友。这样可以根据自己的业务场景选择合适的join语句。
初始化SQL语句:
drop database if exists test;
create database test;
use test;
drop table if exists t1;
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');
drop table if exists t1;
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
1、笛卡尔积
两表关联,把左表的列和右表的列通过笛卡尔积的形式表达出来。
mysql> select * from t1 join t2;
![这里写图片描述](https://img-blog.csdn.net/20170319142335531?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2、左连接
两表关联,左表全部保留,右表关联不上用null表示。
![这里写图片描述](https://img-blog.csdn.net/20170319142752582?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 left join t2 on t1.id = t2.id;
![这里写图片描述](https://img-blog.csdn.net/20170319143100039?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
3、右连接
右表全部保留,左表关联不上的用null表示。
![这里写图片描述](https://img-blog.csdn.net/20170319143202765?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 right join t2 on t1.id =t2.id;
![这里写图片描述](https://img-blog.csdn.net/20170319143346594?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
4、内连接
两表关联,保留两表中交集的记录。
![这里写图片描述](https://img-blog.csdn.net/20170319143445607?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 inner join t2 on t1.id = t2.id;
![这里写图片描述](https://img-blog.csdn.net/20170319143548218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
5、左表独有
两表关联,查询左表独有的数据。
![这里写图片描述](https://img-blog.csdn.net/20170319143724064?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 left join t2 on t1.id = t2.id where t2.id is null;
![这里写图片描述](https://img-blog.csdn.net/20170319143844988?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
6、右表独有
两表关联,查询右表独有的数据。
![这里写图片描述](https://img-blog.csdn.net/20170319143933426?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 right join t2 on t1.id = t2.id where t1.id is null;
![这里写图片描述](https://img-blog.csdn.net/20170319144025225?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
7、全连接
两表关联,查询它们的所有记录。
![这里写图片描述](https://img-blog.csdn.net/20170319144159774?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
oracle里面有full join,但是在mysql中没有full join。我们可以使用union来达到目的。
mysql> select * from t1 left join t2 on t1.id = t2.id
-> union
-> select * from t1 right join t2 on t1.id = t2.id;
![这里写图片描述](https://img-blog.csdn.net/20170319144410521?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
8、并集去交集
两表关联,取并集然后去交集。
![这里写图片描述](https://img-blog.csdn.net/20170319144515693?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
mysql> select * from t1 left join t2 on t1.id = t2.id where t2.id is null
-> union
-> select * from t1 right join t2 on t1.id = t2.id where t1.id is null;
![这里写图片描述](https://img-blog.csdn.net/20170319144757507?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQxMDczMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)