当前位置:网站首页>SQL - Left join, Right join, Inner join
SQL - Left join, Right join, Inner join
2022-07-31 10:37:00 【xiaoweiwei99】
Article Directory
Foreword
I have been doing SQL-related exercises recently, and I found that the SQL I used to be so confident has become tricky if I don't touch it for a long time, especially the internal and external connections between this table.So this article is about sorting out this internal and external connection.
I. Concept
First of all, let's introduce the definitions of these three
1.Left join: that is, left join, which is based on the left table, and connects the two tables according to the conditions of the two tables given after ON.As a result, all the query information in the left table will be listed, while the right table will only list the parts that satisfy the condition after ON and the left table.Left join is called left outer join, which is a type of outer join.
2.Right join: The right join is based on the right table, and connects the two tables according to the conditions of the two tables given after ON.As a result, all the query information in the right table will be listed, while the left table will only list the parts that satisfy the condition after ON and the right table.Right join is called right outer join, which is a kind of outer join.
3.Inner join: that is, inner join, using the two tables as reference objects at the same time, and connecting the two tables according to the conditions of the two tables given after ON.As a result, only the parts of the two tables that satisfy the ON condition will be listed.
There is no full outer join in MySQL, so it is not explained here.
2. Examples
Table table_a

table table_b

----------------------------------------------------------------Dividing line-----------------------------------------------------------------
Left join: left join
SQL Statement:
select *from table_a left join table_b on table_a.id=table_b.id;Results:

Right join: right join
SQL Statement:
select * from table_a right join table_b on table_a.id=table_b.id;Result:
Inner join: Inner join
SQL Statement:
select * from table_a inner join table_b on table_a.id=table_b.id;Result:
Summary
Based on the chestnuts above, it looks much clearer and more contrasting.If you still think that you only understand it now, but you still find it a little difficult to use, you can use the following collective thinking, which I have only recently discovered, and it will be much simpler to think about it this way.
We have two tables: A and B, then treat them as a set respectively
Then our left and right outer joins and inner joins are equivalent to this ↓↓↓
A Left join B-> A (according to the return requirement, it needs to connect the part of B with A and other conditions)
A Right join B -> B (according to the return requirements, the parts of A and B need to be connected)
A Inner join B -> A∩B (the part that needs to be connected with conditions such as A and B according to the return requirement)

Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
随机推荐
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
darknet 训练分类网络
【LeetCode】242. 有效的字母异位词
项目管理工具之燃尽图:动态考核团队工作能力
可以用聚酯树脂将接线板密封接线盒吗?(接线盒灌封胶用哪种树脂)
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
恋爱期间的赠与能否撤销
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
双链表的插入和删除
《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
Redis-基础
Windows系统Mysql8版本的安装教程
Web系统常见安全漏洞介绍及解决方案-XSS攻击
Redis Cluster - Sentinel Mode Principle (Sentinel)
SQL学习笔记——REGEXP运算符
NowCoderTOP23-27 Binary tree traversal - continuous update ing
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
Module eight





![AtCoder—E - Σ[k=0..10^100]floor(X/10^k](/img/be/82cfab00950c1f28d426e76a792906.png)


