Priority
First, clarify two concepts:
In left join, the difference between the two is:
test Table 1: table1
Table 2: table2
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:
|
<<: Manually implement js SMS verification code input box
>>: Solve the problem that VMware15 centos7 bridge mode ssh suddenly cannot be accessed
Table of contents Question: Case (1) fork before ...
The backend uses the thinkphp3.2.3 framework. If ...
All of us webmasters know that when optimizing a ...
In the nginx process model, tasks such as traffic...
Table of contents 1. What is the execution contex...
A simple cool effect achieved with CSS3 animation...
Table of contents 1. some 2. every 3. find 1. som...
Some people use these three tags in a perverted wa...
The <input> tag The <input> tag is us...
This article example shares the specific code of ...
Today, when learning PHP, of course, you have to ...
Look at the code first #/bin/sh datename=$(date +...
Preface I just bought a new VPS. The data disk of...
<template> <div class="demo"&g...
If MySQL version 5.0 already exists on the machin...