当前位置:网站首页>mysql20210906
mysql20210906
2022-07-26 10:45:00 【ZhuMou】
0. Contents summary :
a. The concept of join query , classification
b. Connection query principle , The cartesian product ( Two tables )
c. Two table join query syntax , Equivalent connection , Non equivalent connection , Self join
d. External connection : Left outer connection and right outer connection ; The relationship between outer connection and inner connection
e. Multiple table joins
1. Join query concepts and classifications :
Link query , Or cross table query , Different from the previous single table query , It refers to the combination of multiple tables to query data . According to the grammatical age , It is divided into SQL1992 and SQL1999; According to the connection object , It is divided into internal connection query , External connection query and full connection query ( Leave out ). Internal connection query and External connection query The concept of is put in the external connection query to solve .
2. Connect query principle and Cartesian product phenomenon
Cartesian phenomena occur in unconditional join queries , It embodies the essence of join query . Let's take the internal connection between two tables as an example .
Have a watch emp:

surface dep:

Make internal connections :
select e.deptno, d.dname from emp as e, dep as d;
The final result is 56 Table of rows , The table is too long to show . Look at the table and you can find ,emp Every record in the table is related to dep All in the table 4 Records match the past one by one , This led to the final number of records 14 * 4 = 56 strip . We call this phenomenon Cartesian product phenomenon , That is, the number of records of the table connection result is equal to the product of the number of records of each table .
3. Two table join query syntax
| select field1, field2 from table1,table2; | Unconditionally connect two tables , Leading to Cartesian product |
| select field1,field2 from table1,table2 where condition; | SQL1992, The disadvantage is to mix the inner connection query criteria with the general filter criteria where in , Not now |
| select field1,field2 from table1 join table2 on condition; | SQL1999, It realizes the separation of table join conditions and table filter conditions , Make the structure clearer |
In order to further improve the efficiency of the third statement in the table , We can table1,table2 names , And used in field1 and field2 Before :
select t1.field1,t2.field2 from table1 as t1 inner join table2 as t2 on condition;stay field1 and field2 Specifying the table name prevents mysql At the same time t1 and t2 Search for field1(field2), So it improves efficiency . Otherwise ,mysql Will be in t1 and t2 Search for field1, Will also be in t1 and t2 Search for field2, This reduces efficiency . in addition , added condition after , Although from the results , We avoid the Cartesian product phenomenon . however , The number of matches between the records of the two tables has not decreased , It is still the product of the number of records in each table .
We can think of it like this : The above statement is executed first from and join sentence , Let the two tables be connected into a new table according to the Cartesian product phenomenon , And then execute on sentence , Meet in screening condition Field of . The last is select. therefore , Cartesian product embodies table connection ( Take inner connection as an example ) Principle , Very important .
Now consider condition 了 . For internal connections , If condition Is an equality condition , Then call this internal connection Equivalent connection . for example : Find the working place of each employee .
select e.ename, d.dname from emp as e join dep as d on e.deptno = d.deptno;The condition here is :e.deptno = d.deptno, It's an equation . So this inner connection is an equivalent connection .
In use enterprise Import another table into the database salgrade:

We unite emp and salgrade Find employee salary grade . This requires internal connections Non equivalent connection (condition Is a non equality condition ).
select e.ename, s.grade
from emp as e
join salgrade as s
on e.sal between s.losal and s.hisal;Self join It is a table connected with itself to query data . The idea is to treat one table as two tables . The syntax is consistent with equivalent connection and non equivalent connection , All conform to the syntax of inner connection . for example : Inquire about the leader of the employee .
select e1.ename, e2.ename
from emp as e1
inner join emp as e2
on e1.mgr = e2.empno;4. Two off meter connections
The difference between two tables' external connection and internal connection lies in whether there is a master-slave relationship between two tables . For internal connections , The two tables have the same status ,select After that, only ( Cartesian product ) Qualified records ; For external connections , There is a master-slave relationship between the two tables . The external connection mainly checks the main table , All records of the main table will be displayed , Whether qualified or not ; It will display the qualified records in the table . The grammar is :
// Left connection
select t1.field1, t2.field2 from table1 t1 left join table2 t2 on condition;
// The right connection
select t1.field1, t2.field2 from table1 t1 right join table2 t2 on condition;Left link selected join The table on the left is the main table , On the right is the slave table ; The right connection is the opposite . Left and right connections must be able to transform each other .
Example : Query the leader names of all employees , With or without leadership .
because King There is no leadership , The result should have been 13 Bar record ; But after using the syntax of outer connection , Yes 14 Bar record .
mysql> select e1.ename,e2.ename
-> from emp e1
-> left outer join emp e2
-> on e1.mgr = e2.empno;
Do not conform to the on Later conditional King This record also exists , This is the difference between external connection and internal connection : It displays all records of the master table and qualified slave table records . The outer connection can be considered as the inner connection based on the main table .
5. Multi-table query ( Three watches or above )
grammar :
select
......
from
table1 t1
(inner/left/right)join
table2 t2
on
(t1,t2)condition
(inner/left/right)join
table3 t3
on
(t1,t2,t3)condition
...
...
// The first question is , Every time the condition can only be join The following table name and from Is it composed of the following table name ? This time join Table name after
// And all table names that have appeared before ?
// The experimental results are all acceptable .join tablen tn on (t1,t2,t3,...,tn)In multiple tables , Internal and external connections can be mixed . But here's the thing ,left The connection does not just take the previous table as the main table , Empathy right The connection does not just take the following table as the main table .
Select the employee name , Corresponding department name , Salary and salary grade . It is required to print out all departments .
select e.ename, d.dname, e.sal, s.grade
from emp e
right outer join dep d
on e.deptno = d.deptno
join salgrade s
on e.sal between s.losal and s.hisal;
//e With the first d External connection ,d Main table ; The result is due to s Internal connection , The front outer connection was screened out . So the final result is not what we want .The result is :

Incompatible with the purpose .
Only in this way can we achieve our goal :
mysql> select e.ename, d.dname, e.sal, s.grade
-> from emp e
-> join salgrade s
-> on e.sal between s.losal and s.hisal
-> right outer join dep d
-> on e.deptno = d.deptno
-> \g
or
select e.ename, d.dname, e.sal, s.grade
from emp e
right outer join dep d
on e.deptno = d.deptno
left join salgrade s
on e.sal between s.losal and s.hisal;
I think the reasonable explanation is :( Suppose the table connected with this is table1,table2,...,tablen) First ,table1 Hui He table2 Connect (join), After performing on Screening . The results obtained after screening are compared with table3 Conduct join, We'll do it later on Later conditions . And so on . Of all the join and on After the implementation , perform from.
The rationality of this explanation lies in :1. Conform to language intuition ;2. Connecting after filtering can improve execution efficiency ;3. Can explain the above phenomenon . This also tells us how to mix inner join and outer join in multi table query . If you want to use a table as the main table , Or put it after the beginning left join Or put it at the end right join, Or use it before it right join Use... After it left join.
边栏推荐
- RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)
- 使用定位实现左中右布局,中间内容自适应
- 抽象工厂及其改进示例
- MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常
- The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
- 反射机制简述
- Flutter jni混淆 引入.so文件release包闪退
- 剑指Offer(七):斐波那契数列
- 剑指Offer(四十三):左旋转字符串
- RT-Thread 学习笔记(五)---编辑、下载、调试程序
猜你喜欢
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array

Phase 4: one of College Students' vocational skills preparation in advance
![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming

RT thread learning notes (V) -- edit, download and debug programs

一文详解Nodejs中fs文件模块与path路径模块

RT-Thread 学习笔记(五)---编辑、下载、调试程序

GIS方法类期刊和论文的综述(Introduction)怎么写?

从蚂蚁的觅食过程看团队研发(转载)
![Error[Pe147]: declaration is incompatible with '错误问题](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[Pe147]: declaration is incompatible with '错误问题

20210807 1 c language program structure
随机推荐
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
鹏哥C语言第四课(3)
flutter 背景变灰效果,如何透明度,灰色蒙板遮罩
使用flex实现左中右布局,中间自适应
剑指Offer(九):变态跳台阶
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
LIst和Dictionary实例应用(※)
第7期:内卷和躺平,你怎么选
flutter dart生成N个区间范围内不重复随机数List
反射机制简述
Zongzi battle - guess who can win
Issue 7: how do you choose between curling up and lying flat
Analysis of the transaction problem of chained method call
Flutter 防止科学计数并去除尾数无效0
7-25 0-1背包 (50分)
10 let operator= return a reference to *this
13 managing resources by objects
剑指Offer(二十):包含min函数的栈
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
Issue 5: the second essential skill for College Students