当前位置:网站首页>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.
边栏推荐
- Build ARM embedded development environment
- [leetcode daily question 2021/4/29]403. Frogs cross the river
- Flutter jni混淆 引入.so文件release包闪退
- RT thread learning notes (I) -- configure RT thread development environment
- 反射机制简述
- RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
- 文案秘籍七步曲至----文献团队协作管理
- Happens-Before原则深入解读
- [leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
猜你喜欢

二叉树的遍历 递归+迭代

PLC与伺服电机连接

The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly

文案秘籍七步曲至----文献团队协作管理

RT thread learning notes (III) -- building a compilation environment with scons
![[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)](/img/94/ff52b043320b6dea5ca1238e314de8.png)
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)

242.有效的字母异位词

按二进制数中1的个数分类

用两个栈实现队列

Dry goods likeshop takeout order system is open source, 100% open source, no encryption
随机推荐
MySQL速学笔记-2021-08-31
[转]ArcGIS中判断两个Geometry之间的关系
Error[pe147]: declaration is incompatible with 'error problem
粽子大战 —— 猜猜谁能赢
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
2021-08-14三子棋
SQL Server 之Sql语句创建数据库
Simple use of json-c Library -- converting JSON files to struct
面试知识点
JS对象赋值问题
智能合约dapp系统开发流程技术
Phase 4: one of College Students' vocational skills preparation in advance
27.移除元素
C#委托与匿名方法浅析
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
RT-Thread 学习笔记(一)---配置RT-Thread开发环境
SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
sigmod 函数与softmax 函数对比
MySQL速学-2021-09-01