SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
1.Left join:即左连接,是以左表为基础,根据ON后给出的两表的条件将两表连接起来。结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足的部分。左连接全称为左外连接。
2.Right join:即右连接,是以右表为基础,根据ON后给出的两表的条件将两表连接起来。结果会将右表所有的查询信息列出,而左表只列出ON后条件与右表满足的部分。右连接全称为右外连接。
3.Inner join:即内连接,同时将两表作为参考对象,根据ON后给出的两表的条件将两表连接起来。结果则是两表同时满足ON后的条件的部分才会列出。
表table_a
表table_b
Left join:左连接
select *from table_a left join table_b on table_a.id=table_b.id;
结果:
Right join:右连接
select * from table_a right join table_b on table_a.id=table_b.id;
Inner join:内连接
select * from table_a inner join table_b on table_a.id=table_b.id;
版权声明
本文仅代表作者观点,不代表博信信息网立场。