sql的join分为三种,内连接、外连接、交叉连接。

以下先建2张表,插入一些数据,后续理解起来更方便一些。

create table emp(empno int, name char(20),depart int);

create table depart(dpno int,dpname char(20));

insert into emp values (1,'bell',1);

insert into emp values (2,'smith',2);

insert into emp values (3,'jet',3);

insert into depart values (1,'design');

insert into depart values (2,'database');

insert into depart values (4,'warehouse');

1. 内连接inner join  仅列出两表能按照join条件连接起来的信息,其他的信息不显示

select a.*,b.* from emp a inner join depart b on a.depart=b.dpno;

empno   name         depart       dpno       dpname

----------------------------------------------------------------------

1              bell                1                   1         design

2              smith            2                   2         database

和如下语句得到的信息是一样的:

 select a.*,b.* from emp a,depart b where a.depart=b.dpno;

2. 外连接outer join

(1)左外连接left outer join = left join  显示左表的所有记录,右表符合join条件的信息显示,不符合的置空。

select a.*,b.* from emp a left join depart b on a.depart=b.dpno;

empno   name         depart       dpno       dpname

----------------------------------------------------------------------

1              bell                1                   1         design

2              smith            2                   2         database

3              jet                  3                 

(2)右外连接right outer join = right join 和left join相反,显示右表的所有记录,左表符合join条件的信息显示,不符合的置空。

select a.*,b.* from emp a right join depart b on a.depart=b.dpno;

empno   name         depart       dpno       dpname

----------------------------------------------------------------------

1              bell                1                   1         design

2              smith            2                   2         database

                                                            4         warehouse

(3)全外连接full outer join = full join ,左右两表的信息都全部显示,符合join条件的信息显示,不符合的置空。

select a.*,b.* from emp a full join depart b on a.depart=b.dpno;

empno   name         depart       dpno       dpname

----------------------------------------------------------------------

1              bell                1                   1         design

2              smith            2                   2         database

                                                            4         warehouse

3              jet                  3  

3.交叉连接,也即生成两表的笛卡尔积。得到的记录相当于两表记录的乘积。

select a.*,b.* from emp a cross join depart b ;

empno   name         depart       dpno       dpname

----------------------------------------------------------------------

1              bell                1                   1         design

2              smith            2                   1         design         

3              jet                  3                   1         design

1              bell                1                   2         database

2              smith            2                   2         database         

3              jet                  3                   2         database

1              bell                1                   4         warehouse

2              smith            2                   4         warehouse

3              jet                  3                   4         warehouse

相当于如下语句:

select a.*,b.* from emp a,depart b;

需要注意的是交叉连接后只能跟where,不可以跟on。

如下语句是错的:

select a.*,b.* from emp a cross join depart b on a.depart=b.dpno;

要修改为这样的才正确:

select a.*,b.* from emp a cross join depart b where a.depart=b.dpno;

一般来讲,在大表关联的时候,建议使用inner join或者left join,不建议使用cross join或者where

比如:

select a.*,b.* from emp a,depart b where a.depart=b.dpno ;                                     A     ---不推荐

select a.*,b.* from emp a cross join depart b where a.depart=b.dpno ;                  B     ---不推荐

select a.*,b.* from emp a inner join depart b on a.depart = b.dpno;                         C    ----推荐

这是因为A,B中,sql需要先对两表生成笛卡尔积,然后才根据where后的条件进行判断,而使用C则不需要。所以C较有效率

 

摘自 猫叔的口粮

标签: join用法

添加新评论