MySQL七种JOIN方式

 

1、内连接(Inner Join)

  1.1 取两者的交集,两表共有的

  

select A.*,B.* from A inner join B on A.key = B.key;

  1.2 取两者的差集,两表特独有的

  

select A.* from A left join B on A.key=B.key where B.key IS NULL
UNION
select B.* from A right join B on A.key=B.key where A.key IS NULL

  1.3 取两者的并集,两表的全部

select A.*,B.* from A left join B on A.key=B.key
UNION
select A.*,B.* from A right join B on A.key=B.key

2、左连接(Left Join)

  2.1 取A表的全部 + B表对应数据;

 

select A.*,B.* form A left join B on A.key=B.key;

   2.2 取A表独有的

select A.* from A left join B on A.key=B.key where B.key IS NULL

3、右连接(Right Join)

  3.1 取B表的全部 + A表对应数据;A、B表对换下就是left join了;

select A.*,B.* form A right join B on A.key=B.key;

  3.2 取B表独有的

 

select B.* form A right join B on A.key=B.key where A.key IS NULL;

 

posted @ 2020-03-11 15:53  门虫不是虫  阅读(565)  评论(0编辑  收藏  举报