七秒鱼记忆~
每天进步一点点!!!

join:内连接
left join:左外连接
right join:右外连接
full join/full outer join:全外连接

例如有A表数据如下:

B表数据如下:

join:取两表相同的部分
select * from test.test_a a
join test.test_b b
on a.t_no =b.t_no

left join:取左边表的全部行,没有匹配上的数据用空值填补
select * from test.test_a a
left join test.test_b b
on a.t_no =b.t_no

right join:取右边表的全部行,没有匹配上的数据用空值填补
select * from test.test_a a
right join test.test_b b
on a.t_no =b.t_no

full join:取两表的全部行,两表数据一致的在同一行,两表数据不一致的单独显示一行,没有匹配上的数据用空值填补
select * from test.test_a a
full join test.test_b b
on a.t_no =b.t_no

因此,如果想要取两表不同部分的数据
select * from test.test_a a
full join test.test_b b
on a.t_no =b.t_no
where a.t_no is null or b.t_no is null

等同于:left join + union all + right join
select * from test.test_a a
left join test.test_b b
on a.t_no =b.t_no
where b.t_no is null
union all
select * from test.test_a a
right join test.test_b b
on a.t_no =b.t_no
where a.t_no is null

posted on 2022-09-29 17:34  fishing~  阅读(1135)  评论(0编辑  收藏  举报