测试下mysql的几种连接,加深理解
初始表数据
table1:
table2:
inner join
select * from table1 t inner join table2 t2
9行数据
select * from table1 t inner join table2 t2 on t.Column1 = t2.Column1
**结论:**如果没加匹配条件,那就是全字段匹配;加了匹配条件的话,最终只会保留匹配得上的数据
left join(左外连接:left outer join的缩写)
select * from table1 t left join table2 t2; -- 会报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 200' at line 2
Error position: line: 1
select * from table1 t left join table2 t2 on t.Column1 = t2.Column1 ;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X5zg9Rwr-1680793900171)(D:\Desktop\assets\image-20230406225352043.png)]
**结论:**以左表为基准,右表根据匹配条件,匹配得上的就保留,匹配不上的以null填充
right join(右外连接:right outer join的缩写)
select * from table1 t right join table2 t2 on t.Column1 = t2.Column1
select * from table1 t right outer join table2 t2 on t.Column1 = t2.Column1
**结论:**以右表为基准,左表根据匹配条件,匹配得上的就保留,匹配不上的以null填充