Introduction to the difference between on and where placement conditions in MySQL left join operation

The usage description of the Mysql string interception function SUBSTRING The usage
difference analysis of the left join setting conditions in on and where in MySQL
Add link description
Priority The
same conditions are placed between the two, and the result set may be different because of the priority. The priority of on is higher than where.
First clarify two concepts:

The LEFT JOIN keyword will return all rows from the left table (table_name1), even if there are no matching rows in the right table (table_name2).
When the database returns records by connecting two or more tables, it will generate an intermediate temporary table, and then return this temporary table to the user.
Under left join, the difference between the two:

on is the condition used when generating the temporary table. Regardless of whether the on condition works or not, the row of the left table (table_name1) will be returned.
where is the condition used after the temporary table is generated. At this time, regardless of whether the left join is used, as long as the condition is not true, all are filtered out.
Test
Table 1: table1

id No
1 n1
2 n2
3 n3
表2:table2

No name
n1 aaa
n2 bbb
n3 ccc
1
2
select a.id,a.No,b.name from table1 a left join table2 b on (a.No = b.No and b.name=‘aaa’);
select a.id,a.No,b.name from table1 a left join table2 b on (a.No = b.No) where b.name=‘aaa’;
第一个结果集:

1
2
3
4
5

id No name
1 n1 aaa
2 n2 (Null)
3 n3 (Null)

The second result set:

1
2
3

id No name
1 n1 aaa

The execution process of the first sql: first find the record row of table b whose name is aaa (on (a.No = b.No and b.name='aaa')). Then find the data of a (even if it does not meet the rules of the b table), generate a temporary table and return it to the user.
The second sql execution process: first generate a temporary table, then execute where to filter the result set b.name='aaa' is not true, and finally return to the user.

Because on will filter out the rows that do not meet the conditions first, and then perform other operations, it stands to reason that on is the fastest.

In multi-table query, on works earlier than where. The system first synthesizes multiple tables into a temporary table according to the connection conditions between the tables, and then filters by where, then calculates, and then filters by having after the calculation. It can be seen that in order for the filter condition to play a correct role, you must first understand when the condition should work, and then decide where to put it.

For the associated operations of the tables that JOIN participates in, if the rows that do not meet the join conditions are also in the scope of our query, we must put the join conditions after ON instead of WHERE. If we put the join conditions After WHERE, then all LEFT, RIGHT, and other operations will have no effect. In this case, its effect is completely equivalent to INNER connection. For those conditions that do not affect the selection of rows, you can put them after ON or WHERE.

Remember: all connection conditions must be placed after ON, otherwise all the LEFT, and RIGHT associations in front will be used as decorations, and will not play any role

Guess you like

Origin blog.csdn.net/weixin_42224488/article/details/114136546