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
更多推荐
SQL中join语句详解
发布评论