join,left join,inner join的区别?

Posted liuqing576598117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了join,left join,inner join的区别?相关的知识,希望对你有一定的参考价值。

left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行,等于join
full join,在两张表进行连接查询时,返回左表和右表中所有没有匹配的行;即查询结果是left join和right join的并集

A记录如下:
aID     aNum
1     a20050111
2     a20050112
3     a20050113
4     a20050114
5     a20050115

B记录如下:
bID     bName
1     2006032401
2     2006032402
3     2006032403
4     2006032404
8     2006032408

--------------------------------------------
1.left join
sql语句如下
select * from A
left join B 
on A.aID = B.bID

结果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
5     a20050115    NULL     NULL

3.inner join
sql语句如下
select * from A
inner join B 
on A.aID = B.bID

结果如下:
aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404

 

4.full join
sql语句如下: 
select * from A
full join B 
on A.aID = B.bID

结果如下:

aID     aNum     bID     bName
1     a20050111    1     2006032401
2     a20050112    2     2006032402
3     a20050113    3     2006032403
4     a20050114    4     2006032404
5     a20050115    NULL     NULL
NULL   NULL            8     2006032408

笛卡尔积和inner join结果相同,但是inner join效率快一点join 默认就是 inner join

以上是关于join,left join,inner join的区别?的主要内容,如果未能解决你的问题,请参考以下文章

SQL中left join on 、right join on、inner join on之间的区别

外连接(left join、full join、right join)与内连接(inner join)的区别

inner join left join right join 和full join的区别

关于SQL 查询效率问题 left join 改成 inner join union

MySQL left join right join inner join

SQL的JOIN语法解析(inner join, left join, right join, full outer join的区别)