目录
场景1:得到 “AB交集后和A“ 的并集 (得到A的所有数据+满足某一条件的B的数据)
场景2:得到A减去AB的交集 (A中所有数据减去同时满足B某一条件的数据)
场景1:得到“AB交集后和B“的并集,(场景B的所有数据以及满足某一条件A的数据)
场景2:得到B减去AB的交集。(B中所有的数据减去与A满足同一条件的数据)
场景2:AB的并集减去AB的交集,也就是A,B中不满足某一条件的记录之和
数据基础:A和B数据集
1 内连接
场景:A和B数据 的交集
语法: inner join 或者 join
案例:
select a.*,b.* from table_a
inner join table_b
on a.id = b.id
2 左链接
场景1:得到 “AB交集后和A“ 的并集 (得到A的所有数据+满足某一条件的B的数据)
语法: left join 或者 left outer join
案例:
select a.*,b.* from teablea_a
left join teableb_b
on a.id=b,id
场景2:得到A减去AB的交集 (A中所有数据减去同时满足B某一条件的数据)
语法: left join 或者 left outer join 或者 left join + where b.column is null
案例:
select a.id aid,a.age,b.id bid,b.name from teable a
left join teable b
on a.id=b.id
where b.id is null
3 右链接
场景1:得到“AB交集后和B“的并集,(场景B的所有数据以及满足某一条件A的数据)
语法: right join 或者 right outer join
select a.id aid,a.age,b.id bid,b.name from teable a
right join teable b
on a.id=b.id
场景2:得到B减去AB的交集。(B中所有的数据减去与A满足同一条件的数据)
语法:right join 或者 right outer join + where a.column is null
select a.id aid,a.age,b.id bid,b.name from table a
right join table b
on a.id = b.id
where a.id is null
4 全连接
场景1:得到A和B的并集 (A、B所有的记录)
语法: left join union right join
案例:
select a.id aid,a.age,b.id bid,b.name from teableaa
left join teable b
on a.id =b.id
union
select a.id aid,a.age,b.id bid,b.name from teable a
right join teable b
on a.id =b.id
场景2:AB的并集减去AB的交集,也就是A,B中不满足某一条件的记录之和
语法:left join + is null union right join + isnull
案例:
select a.id aid,a.age,b.id bid,b.name from table a
left join table b
on a.id = b.id
where b.id is null
union
select a.id aid,a.age,b.id bid,b.name from table a
right join table b
on a.id = b.id
where a.id is null
详细图见:https://blog.csdn.net/jintao_ma/article/details/51260458