Mysql中inner join、left join、right join的分析

Mysql中inner join、left join、right join的分析

    先来看一张图(引自百度百科)


下面我们举例分析哈,看表格:

table_A
id a b
1 a1 b1
2 a2 b2
table_B
id a c
1 a1 c1
3 a2 c2

1. inner join :取主从表的交集

select * from table_A as t1 inner join table_B as t2 on t1.id = t2.id;

inner join 结果
table_A.id table_A.a b table_B.id table_B.a c
1 a1 b1 1 a1 c1
2. left join : 取主表加交集
select * from table_A as t1 left join table_B as t2 on t1.id = t2.id;
left join 结果
table_A.id table_A.a b table_B.id table_B.a c
1 a1 b1 1 a1 c1
2 a2 b2 null null null
3. right join : 取从表加交集
select * from table_A as t1 right join table_B as t2 on t1.id = t2.id;
left join 结果
table_A.id table_A.a b table_B.id table_B.a c
1 a1 b1 1 a1 c1
null null null 3 a2 c2





猜你喜欢

转载自blog.csdn.net/qq_42465672/article/details/80856330