本文将对数据库表的连接三种方式:Left join,Right join,Inner join,进行解释总结。
以以下两张表作为example
一、Inner join(内连接)
取出id相同的所有记录:
select * from table_a as a inner join table_b as b on a.a_id = b.b_id;
查询结果:
二、Left join(左连接)
首先取出a表中的所有数据,然后再加上与a,b匹配的数据:
select * from table_a as a left join table_b as b on a.a_id = b.b_id;
查询结果:
三、Right join(右连接)
首先取出b表中所有数据,然后再加上与a,b匹配的的数据
select * from table_a as a RIGHT join table_b as b on a.a_id = b.b_id;
查询结果:
原文:https://www.cnblogs.com/obsession-with-the-young-monk/p/9019427.html