Mysql multi-table query detailed explanation
I. Introduction
2. Example
3. Matters needing attention
I. Introduction
The last part talked about the order of keyword execution in Mysql, only one table is involved; in most cases of practical applications, the query statement will involve multiple tables:
1.1 What are the categories of multi-table joins ?
1.2 What connection methods are available for these categories ?
1.3 Which application scenarios are these connection methods used for ?
This article describes these three points through examples, the purpose is to exhaust all scenarios and all methods, and to give examples of the use of each method.
First, list the classifications (inner joins, outer joins, cross joins) and connection methods used in this article (as follows):
A) Inner join : join, inner join
B)外连接:left join,left outer join,right join,right outer join,union
C) Cross join : cross join
2. The following is an example of analysis
Two Suppose there are two tables A and B, and the table is regarded as a set, then the record in the table is an element in the set.
The two tables are as follows:
TableA:TableB:
2.1 Inner connection (only one scenario)
inner join or join (equivalent to inner join)
- select a.*, b.* from tablea a
- inner join tableb b
- on a.id = b.id
or
- select a.*, b.* from tablea a
- join tableb b
- on a.id = b.id
The result is as follows:
Application Scenario:
In this scenario, the data inside A and B that meet a certain condition is obtained; just because the obtained data is shared internally, the connection method is called inner connection.
2.2 Outer join (six scenarios)
2.2.1 left join 或者left outer join(等同于left join)
- select a.*, b.* from tablea a
- left join tableb b
- on a.id = b.id
or
- select a.*, b.* from tablea a
- left outer join tableb b
- on a.id = b.id
The result is as follows, with more non-existent records in TableB filled with Null:
Application Scenario:
In this scenario, all the data of A and the data of B that meet a certain condition are obtained;
2.2.2 [left join 或者left outer join(等同于left join)] + [where B.column is null]
- select a.id aid,a.age,b.id bid,b.name from tablea a
- left join tableb b
- on a.id = b.id
- Where b.id is null
The result is as follows:
In this scenario, all the data in A minus "data that meets the same conditions as B", and then the remaining data of A is obtained;
2.2.3 right join or fight outer join (equivalent to right join)
- select a.id aid,a.age,b.id bid,b.name from tablea a
- right join tableb b
- on a.id = b.id
Application Scenario:
In this scenario, all the data of B and the data of A that meet a certain condition are obtained;
2.2.4 [left join 或者left outer join(等同于left join)] + [where A.column is null]
- select a.id aid,a.age,b.id bid,b.name from tablea a
- right join tableb b
- on a.id = b.id
- where a.id is null
Application Scenario:
What is obtained in this scenario is all the data in B minus "data that meets the same conditions as A", and then the remaining data of B is obtained;
2.2.5 full join (mysql不支持,但是可以用 left join union right join代替)
- select a.id aid,a.age,b.id bid,b.name from tablea a
- left join tableb b
- on a.id = b.id
- union
- select a.id aid,a.age,b.id bid,b.name from tablea a
- right join tableb b
- on a.id = b.id
应用场景:
这种场景下得到的是满足某一条件的公共记录,和独有的记录
2.2.6 full join + is null(mysql不支持,但是可以用 (left join + is null) union (right join+isnull代替)
- select a.id aid,a.age,b.id bid,b.name from tablea a
- left join tableb b
- on a.id = b.id
- where b.id is null
- union
- select a.id aid,a.age,b.id bid,b.name from tablea a
- right join tableb b
- on a.id = b.id
- where a.id is null
应用场景:
这种场景下得到的是A,B中不满足某一条件的记录之和
注:上面共有其中七(2^3-1)种应用场景,还有一种是全空白,那就是什么都不查,七种情形包含了实际应用所有可能的场景
2.3 交叉连接 (cross join)
2.3.1 实际应用中还有这样一种情形,想得到A,B记录的排列组合,即笛卡儿积,这个就不好用集合和元素来表示了。需要用到cross join:
- select a.id aid,a.age,b.id bid,b.name from tablea a
- cross join tableb b
2.3.2 还可以为cross join指定条件 (where):
- select a.id aid,a.age,b.id bid,b.name from tablea a
- cross join tableb b
- where a.id = b.id
注:这种情况下实际上实现了内连接的效果
三 注意事项
There are still omissions above, that is, the fault tolerance of mysql for sql statements, that is, in the case that sql statements do not fully meet the writing recommendations, mysql will allow this situation and explain it as much as possible:
3.1 Generally, the where condition is added after the cross join, but the use of cross join+on is also interpreted as cross join+where;
3.2 In general, the on qualification needs to be added to the inner connection, such as the above scenario 2.1; if it is not added, it will be interpreted as a cross connection;
3.3 If a comma is used in the connection table, it will be interpreted as a cross connection;
Note: There are also union join and natural inner join in the SQL standard. MySQL does not support it, and it does not make much sense. The results can be obtained by using the above connection methods.
Summary: Summarize all connection methods of mysql, some of which are problems that have not been noticed before, and the usual development is nothing more than these.
PS-1: Since the previous typesetting was not beautiful enough, this article has been reorganized in order to better learn and communicate for everyone