Database Tutorial: The difference between mysql statement writing order and execution order

The writing order and execution order of mysql statements are quite different.

 

Writing order, the general writing order of mysql is:

select

<Data column to be returned>

from

<table name>

join

on

where

group by

<grouping conditions>

having

<Filter conditions after grouping>

order by

<sort condition>

limit

<Line limit>

However, the order of execution is:

from

<table name> # Cartesian product

on

<filter conditions> #Filter the virtual table of the Cartesian product

join

#Specify join, used to add data to the virtual table after on, for example, left join will add the remaining data of the left table to the virtual table

where

#Filter the above virtual table

group by

<grouping conditions> #grouping

#Used for the judgment of the having clause. In writing, this kind of aggregate function is written in the judgment of having

having

<Grouping filter> #Aggregate and filter the grouped results

select

<Return data list> #The returned single column must be in the group by clause, except for aggregate functions

distinct

order by

<sort criteria> #sort

limit

<Line limit>

Part of the explanation:

1. from: select * from table_1, table_2; and select * from table_1 join table_2; The results are the same, both indicate the Cartesian product;

Used to directly calculate the Cartesian product of two tables to get the virtual table VT1, which is the first operation performed by all select statements, and other operations are performed on this table, which is the content completed by the from operation

2. on: Filter the eligible data from the VT1 table to form the VT2 table;

3. Join: add the join type data to the VT2 table. For example, a left join will add the remaining data of the left table to the virtual table VT2 to form a VT3 table; if the number of tables is greater than 2, it will repeat 1-3 step;

4. where: Perform filtering, (aggregate functions cannot be used) to get the VT4 table;

5. group by: group the VT4 table to get the VT5 table; for subsequent processing statements, such as select, having, the columns used must be included in the group by condition, and aggregation functions are needed for those that do not appear;

6. Having: Filter the grouped data to get the VT6 table;

7. select: return the column to get the VT7 table;

8. Distinct: used to remove duplicates to get the VT8 table;

9. order by: used to sort the VT9 table;

10. limit: return the number of rows needed to get VT10;

note:

In the group by condition, each column must be a valid column, not an aggregate function;

The null value will also be returned as a group;

In addition to aggregate functions, the columns in the select clause must be in the group by condition;

 


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/114750906