当前位置:网站首页>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 tab2Results

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.id2Results

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.id2Results

Fourth, right join: RIGHTJOIN
Right join is the exact opposite of left join
SELECT * FROM tab1 RIGHT JOIN tab2 ON tab1.id1 = tab2.id2Results

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.id2But 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.id2Results

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
边栏推荐
猜你喜欢

MySQL分库分表

day11--shell脚本

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers

排序学习笔记(二)堆排序

Problems when yolov5 calls ip camera

二维数组实战项目--------《三子棋》

离线数仓-用户行为采集

@Accessors 注解详解

mysql8.0安装教程与配置(最详细)操作简单
随机推荐
mysql卸载详细教程
MySQL中的各种锁(行锁、间隙锁、临键锁等等LBCC)
Deveco studio 鸿蒙app访问网络详细过程(js)
LeetCode:1161. 最大层内元素和【BFS层序遍历】
线性代数学习笔记3-3:逆矩阵的理解
跨域问题解决
MySQL分区表详解
DOM destruction and reproduction experiment
线性代数学习笔记2-2:向量空间、子空间、最大无关组、基、秩与空间维数
基本运算符
Small program (necessary common sense for development) 1
排序学习笔记(二)堆排序
赶紧进来!!!教你用C语言写三子棋小游戏
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
MySQL分库分表
通过PS 2021 将网页图标抠下来
动态代理工具类
(Reposted) The relationship between hashcode and equals
AttributeError: Can't get attribute 'SPPF' on
二维数组实战项目--------《三子棋》