SQL中join语句详解

1.inner join(内连接)

只返回匹配的行。
内连接
select * from table_a a inner join table_b b on a.name = b.name

2.left join(左外连接)

返回左表的全部数据,和右表中满足on条件的行,如果左表的行在右表中没有匹配的数据,那么这一行中右表对应的数据为null。
左外连接
select * from table_a a left join table_b b on a.name = b.name

3.right join(右外连接)

返回右表中所有的行,和左表中满足on条件的行,如果右表的行在左表中没有匹配,那么这一行中左表的对应数据为null。
右外连接
select * form table_a a right join table_b b on a.name = b.name

4.left join excluding inner join(左连接-内连接)

就是在left join查询中加入一个where条件(b.name is null),过滤掉A,B表的交集。
left join 会返回左表的全部,右表中匹配的返回,不匹配的null值填充,本查询刚好过过滤掉右表为null的记录

在这里插入图片描述
select * from table_a a left join table_b b on a.name = b.name where b.name is null

5.right join excluding inner join(右连接-内连接)

就是在right join查询中加入一个where条件(a.name is null),过滤掉A,B表的交集。
right join 会返回右表的全部,左表中匹配的返回,不匹配的用null填充,本查询刚好过滤掉左表为null的记录

在这里插入图片描述
select * from table_a a right join table_b b on a.name = b.name where a.name is null

**

注意MySQL不支持,full outer join

**

6.full outer join(外连接)

会返回左表,右表所有的行,对应表中没有数据以null填充。
在这里插入图片描述
由于mysql数据库并不支持full outer join,所以需要使用left join + left join来代替
select * from table_a a left join table_b b on a.name = b.name
union
select * from table_a a right join table_b b on a.name = b.name

7.full outer join excluding inner join(外连接-内连接)

在这里插入图片描述
select * from table_a a left join table_b b on a.name = b.name where b.name is null
union
select * form table_a a right join tablb_b b on a.name = b.name where a.name is null

猜你喜欢

转载自blog.csdn.net/qq_42182034/article/details/107535320