当前位置:网站首页>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
边栏推荐
猜你喜欢

Redis缓存面临的缓存穿透问题

Mybaits 常用问题详解

单点登录的三种方式

如何判断自己是否适合IT行业?方法很简单

darknet 硬件软件环境的设置和检测

Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1

学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架

新人学习小熊派华为iot介绍

Experience innovation and iteration through the development of a lucky draw applet

web安全入门-黑苹果MAC系统安装
随机推荐
Gradle系列——Groovy概述,基础使用(基于Groovy文档4.0.4)day2-1
Redis缓存面临的缓存雪崩问题
Mybaits Frequently Asked Questions Explained
odoo14 | 附件上传功能及实际使用
Redis-基础
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
Sql优化总结!详细!(2021最新面试必问)
【LeetCode】141.环形链表
SQLServer2019安装(Windows)
darknet 训练分类网络
darknet 源码阅读笔记-01-activation_kernels.cu
我们能做出来数据库吗?
unity-shader-2
darknet 硬件软件环境的设置和检测
What is the encoding that starts with ?
Burndown chart of project management tools: Dynamic assessment of team work ability
sql力扣刷题八
KVM虚拟化作业
csdn file export to pdf
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘