Introduction When writing SQL today, I encountered a problem. The requirement is to query the data and sort it in reverse order of the score and the number of visits in the past week. The problem is that the conventional way of writing is to put the day >= xxx condition in the where clause. If some data has no visits in the past week, then this data cannot be found. The solution is to put the condition in LEFT JOIN. MySQL statement execution order First of all, let me explain a concept. The order in which MySQL statements are executed is not the order of SQL statements. Here is the sample SQL SELECT DISTINCT < select_list > FROM < left_table > < join_type > JOIN < right_table > ON < join_condition > WHERE < where_condition > GROUP BY < group_by_list > HAVING < having_condition > ORDER BY < order_by_condition > LIMIT < limit_number > The following is the SQL execution order FROM <left_table> ON <join_condition> <join_type> JOIN <right_table> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> SELECT DISTINCT <select_list> ORDER BY <order_by_condition> LIMIT <limit_number> The role of LEFT JOIN The difference in result sets is not only related to the SQL priority, but also to LEFT JOIN When using left join, the condition after on is only valid for the right table.
The above is excerpted from two materials, which can summarize it well (the original links are below, and there are examples in them). References:
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:
|
<<: Installation and configuration tutorial of Linux virtual machine under Windows operating system
>>: Vue Element-ui table realizes tree structure table
This article uses an example to describe how MySQ...
This article shares two methods of implementing t...
Recently, when I was working on a conference heal...
During the front-end development process, a situat...
Table of contents What is the Picker component Pr...
Table of contents How to rename MySQL database Th...
Table of contents Write docker-compose.yml Run do...
Take the deployment of https://gitee.com/tengge1/...
Preface I am used to writing less/sass, but now I...
Table of contents Effect demonstration:Main JS co...
In MySQL, the LOAD_FILE() function reads a file a...
When the amount of data in MySQL is large, limit ...
background Speaking of MySQL deadlock, I have wri...
Table of contents 1. Install html2Canvas 2. Intro...
Table of contents Preface Installation and Usage ...