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查询的主要内容,如果未能解决你的问题,请参考以下文章