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
Convert code to image using html2canvas is a very...
This article example shares the specific code of ...
This article uses an example to describe the solu...
border-radius:10px; /* All corners are rounded wi...
Related articles: Beginners learn some HTML tags ...
This article example shares the specific code of ...
If every company wants to increase its user base,...
Mysql converts query result set into JSON data Pr...
Web design is both a science and an art. Web desi...
Why are the scroll bars of the browsers and word ...
After installing Navicat The following error may ...
Read uncommitted example operation process - Read...
Table of contents Preface zx library $`command` c...
Table of contents Preface Introduction to Session...
Table of contents First method App.vue Home.vue H...