MySQL中的七种常见通用的join查询

Posted 活跃的咸鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL中的七种常见通用的join查询相关的知识,希望对你有一定的参考价值。

常见通用的join查询

在这里插入图片描述
在这里插入图片描述

我用下面两张表来演示上面七种情况:

t_nation表
在这里插入图片描述
t_hero表
在这里插入图片描述

1.A∩B (内连接 B图)

select * from t_hero as h join
 t_nation n on h.hc_id=n.n_id;

在这里插入图片描述

2.A-B ( D图)

select * from t_hero as h left join 
t_nation as n on h.hc_id=n.n_id where n.n_id is null;

在这里插入图片描述

3.B-A (E图)

select * from t_hero as h right join 
t_nation as n on h.hc_id=n.n_id where h.h_id is null;

在这里插入图片描述

4.A∪B (F图 全外连接 mysql不支持)

select * from t_hero as h right join 
t_nation as n on h.hc_id=n.n_id
union
select * from t_hero as h left join 
t_nation as n on h.hc_id=n.n_id;

在这里插入图片描述

5.A-B∪ A∩B (A图 左连接)

select * from t_hero as h left join
 t_nation as n  on h.hc_id=n.n_id 

在这里插入图片描述

6.B-A∪ A∩B (B图 右连接)

select * from t_hero as h right join
 t_nation as n  on h.hc_id=n.n_id 

在这里插入图片描述

7.A-B∪B-A (G图)

select * from t_hero as h right join
 t_nation as n  on h.hc_id=n.n_id where h.hc_id is null
union
select * from t_hero as h left join 
t_nation as n  on h.hc_id=n.n_id where n.n_id is null;

在这里插入图片描述

以上是关于MySQL中的七种常见通用的join查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL常用的七种join查询

MySQL常用的七种join查询

MySQL常用的七种join查询

MySQL 的七种 Join

MYSQL 的七种join

知识库-数据库_MySQL 的七种 join