The SQL query statement execution order is as follows: (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_type> JOIN <right_table> (2) ON <join_condition> (4) WHERE <where_condition> (5) GROUP BY <group_by_list> (6) HAVING <having_condition> (9) ORDER BY <order_by_condition> (10) LIMIT <limit_number> Preliminary preparations 1. Create a new test database create database testData; 2. Create a test table and insert data as follows: User Table Order Form Prepare SQL logical query test statements SELECT a.user_id,COUNT(b.order_id) as total_orders FROM user as a LEFT JOIN orders as b ON a.user_id = b.user_id WHERE a.city = 'beijing' GROUP BY a.user_id HAVING COUNT(b.order_id) < 2 ORDER BY total_orders desc Use the above SQL query to get customers from Beijing who have less than 2 orders; During the execution of these SQL statements, a virtual table will be generated to save the execution results of the SQL statements. 1. Execute the FROM statement The first step is to execute the After the FROM statement performs a Cartesian product on the two tables, a virtual table, VT1 (vitual table 1), is obtained, with the following content: There are 28 (number of user records * number of orders records) records in total. This is the result of VT1. The following operations are based on VT1. 2. Execute ON filtering After executing the Cartesian product, select * from user as a inner JOIN orders as b ON a.user_id = b.user_id; 3. Add external rows This step only occurs when the join type is
Since I used LEFT JOIN in the prepared test SQL query logic statement, the resulting VT3 table is as follows: 4. Execute where condition filtering The data with the added external rows is filtered by the where condition. Only the records that meet the <where_condition> condition will be filtered out. Execute WHERE a.city = 'beijing' to get VT4 as follows: However, when using the WHERE clause, you need to pay attention to the following two points: 1. Since the data has not been grouped yet, you cannot use where_condition=MIN(col) in the where filter condition to filter group statistics. 2. Since the column selection operation has not been performed, the use of column aliases in the select is also not allowed. For example, select city as c from table1 where c='beijing' is not allowed. 5. Execute the group by statement 6. Execution of having The 7. Select List The We execute 8. Execute distinct to deduplicate data If the 9. Execute the order by clause Sort the contents of the virtual table VT7 by the specified column, and then return a new virtual table. We execute DESC sorts in descending order, ASC sorts in ascending order 10. Execute the limit statement The LIMIT clause selects the specified row data starting from the specified position from the virtual table obtained in the previous step, which is often used for paging; LIMIT of MySQL database supports the following options: limit n,m Indicates selecting m records starting from the nth record. For small data, there is no problem with using the LIMIT clause. However, when the amount of data is very large, using LIMIT n, m is very inefficient. Because the LIMIT mechanism is to scan from the beginning each time, if you need to read 3 pieces of data starting from the 600,000th row, you need to first scan to the 600,000th row and then read it. The scanning process is a very inefficient process. 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:
|
<<: Detailed explanation of the principle of Docker image layering
>>: Example of using rem to replace px in vue project
I designed and customized a navigation bar with a...
Table of contents pom configuration Setting.xml c...
Introduction to HTML HyperText Markup Language: H...
Introduction When we use the MySQL database, we a...
Table of contents 1. Download MySQL 1.1 Download ...
FIFO communication (first in first out) FIFO name...
Table of contents 1. Create components using func...
Table of contents Cross-domain reasons JSONP Ngin...
1. useState: Let functional components have state...
Recently I have been saying that design needs to h...
About password strength verification: [root@mysql...
Table of contents Preface webpack-deb-server webp...
Everything is a file! UNIX has already said it. E...
1. Environmental Preparation CentOS Linux release...
Table of contents Understanding Prototypes Unders...