当前位置:网站首页>Usage of JOIN in MySQL
Usage of JOIN in MySQL
2022-08-02 03:34:00 【asdfadafd】
JOIN is used for join table query, there are 5 main usages.The five usages are demonstrated below.
Create 2 tables at will, the structure is as follows
The field col1 is used to make the two tables have a field with the same name (but it is actually useless, because the query conditions need to be specified with ON, here is just to explain that if there is the same field name, it does not matter)
I. Cartesian product: CROSS JOIN
CROSS JOINAll fields of the two tables are directly Cartesian product, assuming that there are m pieces of data in table 1 and n pieces of data in table 2, the number of results is m*n piecesp>
SELECT * FROM tab1 CROSS JOIN tab2
Results
Second, inner join: INNER JOIN
The inner join needs to use ON to specify the fields to be compared between the two tables, and the final result only displays the data that meets the conditions
SELECT * FROM tab1 INNER JOIN tab2 ON tab1.id1 = tab2.id2
Results
Note that Inner joins only connect data that satisfy the ON condition, which is different from Cartesian product
Third, left join: LEFTJOIN
Left join can be regarded as the basis of inner join, and the data in the left table that does not meet the ON condition is also displayed, but the data in the right table part of the result is NULL
SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.id1 = tab2.id2
Results
Fourth, right join: RIGHTJOIN
Right join is the exact opposite of left join
SELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2
Results
5. Full connection: OUTERJOIN
Full join is the union of left join and right join, but MySQL does not support full join writing
SELECT * FROM tab1 OUTER JOIN tab2 ON tab1.id1 = tab2.id2
But you can use UNION to combine the result of left join and right join instead
SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.id1 = tab2.id2UNIONSELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2
Results
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 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
边栏推荐
- Keil development environment installation tutorial
- debian 10 nat 与路由转发
- @Autowired注解的使用
- 【面试】失败的一次面试
- 磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
- day11--shell脚本
- Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
- mysql8.0安装教程与配置(最详细)操作简单
- RHCSA第二天
- redis进行持久化时,有新的写操作将如何解决——写时复制
猜你喜欢
随机推荐
支付通道对接常见的问题有哪些?
MySQL中JOIN的用法
删库后!除了跑路还能干什么?
Mysql8.0安装教程
getattr()函数解析
day11--shell脚本
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
STL entry basics map and set containers
多个el-select下拉框无法选中相同内容
磷脂-聚乙二醇-醛基 DSPE-PEG-Aldehyde DSPE-PEG-CHO MW:5000
JJWT工具类
LeetCode:1161. 最大层内元素和【BFS层序遍历】
ModuleNotFoundError No module named ‘xxx‘可能的解决方案大全
错误:with open(txt_path,‘r‘) as f: FileNotFoundError: [Errno 2] No such file or directory:
线性代数学习笔记3-1:矩阵与线性变换、常见矩阵(逆矩阵、伴随矩阵、正交矩阵等)
JJWT tool class
Freeswitch操作基本配置
Double Strings (don't always forget substr)
分布式领域最重要的一篇论文,到底讲了什么?
CV-Model [4]: MobileNet v3