join、Inner join、left join、right join、full join的使用

1、新建两张表,如下图所示:



 

 其中,persons表中的id与orders表中的pid相对应。

2、join

select a.*,b.* from persons a
join orders b
on a.id = b.pid;

查询结果为满足a.id和b.pid的数据行,其他的不显示,如下图所示:

 2、inner join 内连接

select a.*,b.* from persons a
INNER JOIN orders b
on a.id = b.pid;

查询结果与join一样,为满足a.id和b.pid的数据行,其他的不显示,

其实join为inner join省略掉inner,如下图所示:



 3、外连接 left join、right join、full join,其实是left outer join、right outer  join、full outer  join,只是outer 可以省略掉而已。

select a.*,b.* from persons a
LEFT OUTER JOIN orders b
on a.id = b.pid;

左连接,则返回的数据是以左边的数据表为基准,即以left join前面的数据表为基准,当左表格中的行在右表中没有匹配行时,也返回。返回的记录中,选择的右表的列的内容为NULL

如下图所示:



 4、右连接

则返回的数据是以右边的数据表为基准,即以RIGHT join后面的数据表为基准,当右表格中的行在左表中没有匹配行时,也返回。返回的记录中,选择的左表的列的内容为NULL

如下图所示:



 5、FULL JOIN

可以把它理解为LFETRIGHT的集合,某表中某一行在另一表中无匹配行,则相应列的内容为NULL。如下图所示:



 

猜你喜欢

转载自jiage17.iteye.com/blog/2175103