天天看点

left join、right join、full join、inner join区别

1、left join(左链接)

作用:以左表为主,查询左表的全部数据,右表展示与左表相关联的部分,也就是有交集的部分。

left join、right join、full join、inner join区别

select * from staff s left join company c on s.company_id = c.company_id;

2、right join (右链接)

作用:以右表为主,查询右表的全部数据,左表展示与右表相关联的部分,有交集的部分。

left join、right join、full join、inner join区别

select * from staff s right join company c on s.company_id = c.company_id;

3、full join(全链接)

作用:将左右两张表查询的结果合并到一块儿。

left join、right join、full join、inner join区别

select * from staff s full join company c on s.company_id = c.company_id;

4、inner join(内链接)

作用:查询左右两张表,展示出有交集的部分,没有交集的就不展示。

left join、right join、full join、inner join区别

select * from staff s inner join company c on s.company_id = c.company_id;

select * from staff s join company c on s.company_id = c.company_id;

注:1、如果前面啥也不写只写join,就相当于是inner join

2、full join 是Oracle的,MySQL是没有的,如果MySQL想查合并的话可以用左链接+union+右链接。

这里的union的作用是将两个结果集合并成一个结果集。

select * from staff s left join company c on s.company_id = c.company_id

union

select * from staff s right join company c on s.company_id = c.company_id;