Analysis of the difference between the usage of left join setting conditions in on and where in mysql

Analysis of the difference between the usage of left join setting conditions in on and where in mysql

This article uses examples to illustrate the difference between the usage of on and where conditions for left join in MySQL. Share with you for your reference, the details are as follows:

1. First, we prepare two tables for testing.

CREATE TABLE `a` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 `name` varchar(32) DEFAULT '' COMMENT 'name',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `b` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
 `a_id` int(11) DEFAULT '0' COMMENT 'a table ID',
 `name` varchar(32) DEFAULT '' COMMENT 'name',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

The data of the two tables are shown in the figure:

Run the following left join query:

select * from a left join b on a.id = b.a_id;

We add conditions after on and where respectively to see whether the execution results are the same.

select * from a left join b on a.id = b.a_id and b.id > 3;

select * from a left join b on a.id = b.a_id where b.id > 3;

In the above two statements, the conditions we set are the same, both are b.id > 3, why the displayed results are different.

The keyword sequence of SQL statement query is generally from > where > group by > having > order by

When left join is in the from range, the on condition will first filter the right table of the left join, and then filter the results of the where condition.

Multiple left joins will generate a temporary table. The on condition is to filter the left join right table, and the where condition is to filter the last generated temporary table.

so:

If the condition b.id > 3 is written after on, the right table (related table) is first screened to obtain the rows that meet the conditions, and then the main table is left joined to return all the rows of the main table. The rows that are not matched in the right table are represented by null.

If the condition b.id > 3 is written after where, the main table is left joined with the right table (associated table) to return all rows, and then the where condition is used to filter the results.

Note: The condition after on is for the table on the right (the associated table) and has no effect on the main table.

select * from a left join b on a.id = b.a_id and a.id > 3;

We added the condition a.id > 3 for the main table after on, but all the data in the main table is still displayed, but it affects the display of the table on the right (the associated table).

If you want to filter the main table, you should write the condition after where.

select * from a left join b on a.id = b.a_id where a.id > 3;

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

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

<<:  What to do after installing Ubuntu 20.04 (beginner's guide)

>>:  Practical TypeScript tips you may not know

Recommend

In-depth understanding of the implementation principle of require loader

Preface We often say that node is not a new progr...

How to represent various MOUSE shapes

<a href="http://" style="cursor...

Analysis of Facebook's Information Architecture

<br />Original: http://uicom.net/blog/?p=762...

JS interview question: Can forEach jump out of the loop?

When I was asked this question, I was ignorant an...

Management of xinetd-based services installed with RPM packages in Linux

Table of contents Preface 1. Startup management b...

Reasons and solutions for prompting to save action after uploading files in form

The json data must be returned in html format That...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

What are the new CSS :where and :is pseudo-class functions?

What are :is and :where? :is() and :where() are p...

Solve the problem of resetting the Mysql root user account password

Problem description: The following error message ...

Pure CSS code to achieve flow and dynamic line effects

Ideas: An outer box sets the background; an inner...

The difference between html Frame, Iframe and Frameset

10.4.1 The difference between Frameset and Frame ...

How to deal with too many Docker logs causing the disk to fill up

I have a server with multiple docker containers d...

CentOS 7 builds hadoop 2.10 high availability (HA)

This article introduces how to build a high-avail...

Several commonly used single-page application website sharing

CSS3Please Take a look at this website yourself, ...