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

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

Priority

The reason why placing the same condition in both cases may result in different result sets is because of the priority. The priority of on is higher than that of where.

First, clarify two concepts:

  • The LEFT JOIN keyword returns 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 generates an intermediate temporary table and then returns this temporary table to the user.

In left join, the difference between the two is:

  • on is the condition used when generating a temporary table. Regardless of whether the on condition works or not, the rows of the left table (table_name1) will be returned.
  • Where is the condition used after the temporary table is generated. At this time, it doesn’t matter whether left join is used or not. All rows that do not meet the condition will be filtered out.

test

Table 1: table1

id No
1 n1
2 n2
3 n3

Table 2: table2

No name
n1 aaa
n2 bbb
n3 ccc
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';

First result set:

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

Second result set:

|id |No |name|
|---|---|---|
|1 |n1 |aaa|

The execution process of the first SQL statement is: first find the row in table b where name is aaa (on (a.No = b.No and b.name='aaa') ). Then find the data of table a (even if it does not meet the rules of table b), generate a temporary table and return it to the user.

The execution process of the second SQL statement is as follows: first, a temporary table is generated, then the where statement is executed to filter the result set where b.name='aaa' is not true, and finally the result is returned to the user.

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

When querying multiple tables, on takes effect earlier than where. The system first combines multiple tables into a temporary table based on the join conditions between the tables, then filters it using where, and then calculates it. After the calculation, it is filtered again using having. It can be seen from this that in order for the filtering conditions to play a correct role, we must first understand when the condition should take effect, and then decide where to put it.

For the association operation of the tables involved in JOIN, if the rows that do not meet the connection conditions are required to be within our query range, we must put the connection conditions after ON, not after WHERE. If we put the connection conditions after WHERE, then all LEFT, RIGHT, etc. operations will not have any effect. In this case, its effect is exactly the same as INNER connection. For those conditions that do not affect the selection of rows, just put them after ON or WHERE.

Remember: all connection conditions must be placed after ON, otherwise all the previous LEFT and RIGHT associations will be used as decoration and will not have any effect.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Analyzing the difference between using on and where filtering in MySQL left (right) join
  • Analysis of the difference between placing on and where in MySQL query conditions
  • Analysis of the difference between the usage of left join setting conditions in on and where in mysql
  • Detailed explanation of the difference between ON and Where in MySQL

<<:  Manually implement js SMS verification code input box

>>:  Solve the problem that VMware15 centos7 bridge mode ssh suddenly cannot be accessed

Recommend

Introduction to fork in multithreading under Linux

Table of contents Question: Case (1) fork before ...

How to configure pseudo-static and client-adaptive Nginx

The backend uses the thinkphp3.2.3 framework. If ...

Analysis of the difference between bold <b> and <strong>

All of us webmasters know that when optimizing a ...

Detailed explanation of the use of shared memory in nginx

In the nginx process model, tasks such as traffic...

Detailed explanation of execution context and call stack in JavaScript

Table of contents 1. What is the execution contex...

Sample code for cool breathing effect using CSS3+JavaScript

A simple cool effect achieved with CSS3 animation...

Details on using JS array methods some, every and find

Table of contents 1. some 2. every 3. find 1. som...

Use thead, tfoot, and tbody to create a table

Some people use these three tags in a perverted wa...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

js to realize simple shopping cart function

This article example shares the specific code of ...

Complete steps for mounting a new data disk in CentOS7

Preface I just bought a new VPS. The data disk of...

Detailed explanation of Vue's props configuration

<template> <div class="demo"&g...

Mysql 5.6.37 winx64 installation dual version mysql notes

If MySQL version 5.0 already exists on the machin...