阅读导航
一、概要
二、join分类
三、join分类详解
一、概要
join对于接触过数据库的人,这个词都不陌生,而且很多人很清楚各种join,还有很多人对这个理解也不是很透彻,这次就说说join操作。
图片是很容易被接受和理解,所以尝试使用图片来说明一下。
二、join分类
客官:小二,上join分类!
……
小二:客官,新鲜出炉的join分类图片来喽。
三、join分类详解
客官:小二,速速详细道来!
小二:现在让小二来给您详细介绍。
inner join:
仅仅返回两个表中,匹配列相同的列值,所在行的数据。
select * from table1 t1 inner join table2 t2 on t1.col1 = t2.col1
left outer join:
左外连接:返回左表的所有数据,并且在右表中不能匹配的列值,其坐在行则使用空值。
select * from tables1 t1 left outer join table2 t2 on t1.col1 = t2.col2
left outer join - where null:
返回和右表不匹配的所有数据行
select * from table1 t1 left outer join table2 t2 on t1.col1 = t2.col1 where t2.col1 is null
right outer join:
右外连接:返回右表的所有数据,并且在左表中不能匹配的列值,其所做在行则使用空值。
select * from tables1 t1 right outer join table2 t2 on t1.col1 = t2.col2
right outer join – where null:
返回和左表不匹配的所有数据行。
select * from table1 t1 right outer join table2 t2 on t1.col1 = t2.col1 where t1.col1 is null
full outer join:
完全连接可看作是左外连接和右外连接结果之和,返回两个表的所有数据,如果匹配列的值在两个表中匹配,那么返回数据行,否则返回空值。
select * from table1 t1 full outer join table2 t2 on t1.col1 = t2.col1
full outer join – where null:
返回内连接以外的数据行,即匹配列坐在行以外的所有数据。
select * from table1 t1 full outer join table2 t2 on t1.id = t2.id where t1.id is null or t2.id is null
cross join:
交叉连接不需要任何连接条件。这个会把两个表的的数据进行笛卡尔积操作。
select * from table1 t1 cross join table2 t2
小二:小二已经介绍完毕,客官,请慢用。准备洗漱睡觉了。
