Analysis of the difference between placing on and where in MySQL query conditions

Analysis of the difference between placing on and where in MySQL query conditions

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.

  • 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.

The above is excerpted from two materials, which can summarize it well (the original links are below, and there are examples in them).

References:

  • Step by step: Overview of MySQL architecture -> Query execution process -> SQL parsing order
  • The difference between the on and where conditions in MySQL left join operations
  • The difference between placing filter conditions in on and where in SQL.

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:
  • MySQL retrieves data based on the JSON field content as a query condition (including JSON arrays)
  • The difference and reasons between the MySQL query conditions not in and in
  • Detailed explanation of the problem of matching even when there is a space at the end of the string in the Mysql query condition
  • Detailed explanation of common usage of MySQL query conditions
  • Will the index be used in the MySQL query condition?
  • MySQL explains how to optimize query conditions

<<:  Installation and configuration tutorial of Linux virtual machine under Windows operating system

>>:  Vue Element-ui table realizes tree structure table

Recommend

How to install PHP7.4 and Nginx on Centos

Prepare 1. Download the required installation pac...

Detailed steps for installing MinIO on Docker

Table of contents 1. Check whether the docker env...

JavaScript implements three common web effects (offset, client, scroll series)

Table of contents 1. Element offset series 2. Ele...

WeChat applet realizes linkage menu

Recently, in order to realize the course design, ...

MySQL incremental backup and breakpoint recovery script example

Introduction Incremental backup means that after ...

Vue3 Vue CLI multi-environment configuration

Table of contents 1. Introduction 2. Switching 1....

What are the benefits of using // instead of http:// (adaptive https)

//Default protocol /The use of the default protoc...

Summary of the use of special operators in MySql

Preface There are 4 types of operators in MySQL, ...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...

my.cnf (my.ini) important parameter optimization configuration instructions

MyISAM storage engine The MyISAM storage engine i...

The linkage method between menu and tab of vue+iview

Vue+iview menu and tab linkage I am currently dev...

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...