MySQL statement execution order and writing order example analysis

MySQL statement execution order and writing order example analysis

The complete syntax of the select statement is:

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>

Execution order:

from →join →on →where →group by →having →select →order by →limit

(1) The role of each keyword:

from: From which data table do you want to retrieve data? If there is a join, perform a Cartesian product (cross join) on the first two tables in the FROM clause to generate a temporary table (n×m rows)

on: Conditionally filter the above temporary tables

left/right (join): Supplement the left or right table to keep it complete. If there are multiple associated tables, the intermediate table continues the above two steps for the next table.

  • where: Conditions for filtering data in the table
  • group by: How to group the filtered data above

sum: aggregate function

  • Having: Conditions for filtering the grouped data above
  • select: View which column in the result set, or the calculation result of the column

distinct:

  • order by: the order in which to view the returned data
  • limit: Limit the number of query results returned

(2) The difference between on and where:

  • The screening conditions after a.on are mainly for related tables [and are not applicable to the main table screening conditions].
  • b. If you want to filter after the connection is completed, you should put the condition after where. We need to treat association tables differently. If you want to connect after conditional query, you should put the query after on.
  • c. The filter condition for the main table should be placed after where, not after on.

(3) The difference between having and where:

  • a.having can only be used after group by to filter the grouped results (that is, the prerequisite for using having is grouping).
  • b.where must come before group by, that is, before having.
  • Aggregate functions are not allowed in the conditional expression after where, but having is allowed.

(4) Usage of count

When using count(column name) and a null value appears in a column, count(*) will still be calculated, but count(column name) will not.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Analysis of the execution order of T-SQL query statements
  • In-depth explanation of SQL statement execution (MySQL architecture overview -> query execution process -> SQL parsing order)
  • Django executes native MySQL statements to implement process analysis
  • The process and principle of SQL statement parsing and execution

<<:  After Webpack-cli is successfully installed, check the webpack -v error case for details

>>:  A brief discussion on how to modify/set the environment variable JAVA_HOME under Linux

Recommend

js+Html to realize table editable operation

This article shares the specific code of js+Html ...

Understanding of haslaylout and bfc parsing

1. haslayout and bfc are IE-specific and standard ...

Docker Detailed Illustrations

1. Introduction to Docker 1.1 Virtualization 1.1....

Detailed explanation of redo log and undo log in MySQL

The most important logs in the MySQL log system a...

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...

Detailed explanation of the entry-level use of MySql stored procedure parameters

Use of stored procedure in parameters IN paramete...

Example of how to deploy a Django project using Docker

It is also very simple to deploy Django projects ...

How to use mysqladmin to get the current TPS and QPS of a MySQL instance

mysqladmin is an official mysql client program th...

HTML tutorial, understanding the optgroup element

Select the category selection. After testing, IE ...

Reasons and methods for Waiting for table metadata lock in MySQL

When MySQL performs DDL operations such as alter ...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

MySQL's conceptual understanding of various locks

Optimistic Locking Optimistic locking is mostly i...

Display special symbols in HTML (with special character correspondence table)

Problem Reproduction When using HTML for editing,...