当前位置:网站首页>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
边栏推荐
- GVINS论文阅读笔记
- 【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
- Rich text editor Tinymce
- Module eight
- nodeJs--url模块
- Build finished with errors/Executable Not Found
- 【LeetCode】73.矩阵置零
- Open Kylin openKylin automation developer platform officially released
- Hospital management system database, course design, SQLserver, pure code design
- unity-shader-2
猜你喜欢
随机推荐
Build finished with errors/Executable Not Found
Three ways of single sign-on
Module eight
darknet 训练分类网络
Chapter VII
Experience innovation and iteration through the development of a lucky draw applet
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
GCD简单了解
csdn file export to pdf
Come n times - 07. Rebuild the binary tree
浅谈Attention与Self-Attention,一起感受注意力之美
P5231 [JSOI2012]玄武密码(SAM 经典运用)
[ verb phrase ] collection
【GORM】存取数组/自定义类型数据
darknet 源码阅读笔记-01-activation_kernels.cu
Web系统常见安全漏洞介绍及解决方案-XSS攻击
乐观锁和悲观锁
业务-(课程-章节-小节)+课程发布一些业务思路
Sql优化总结!详细!(2021最新面试必问)
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘