当前位置:网站首页>Tutorial on the principle and application of database system (050) -- MySQL query (XII): analysis of the execution process of select command
Tutorial on the principle and application of database system (050) -- MySQL query (XII): analysis of the execution process of select command
2022-07-24 00:50:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (050)—— MySQL Inquire about ( Twelve ):SELECT Analysis of the execution process of the command
Catalog
- Database system principle and Application Tutorial (050)—— MySQL Inquire about ( Twelve ):SELECT Analysis of the execution process of the command
SELECT A statement consists of several parts , You need to follow a certain order when writing , Otherwise, a syntax error will be prompted . In the process of execution , Each part has corresponding execution sequence , When using column aliases, you need to pay attention to the execution order of each part , Otherwise, the alias will not be found .
One 、SELECT The writing order of sentences
SELECT The writing order of each component of the sentence is as follows :
SELECT DISTINCT < Column name or expression list >
FROM <table_name> [INNER|LEFT|RIGHT] JOIN <table_name>
ON < Connection condition >
WHERE < filter >
GROUP BY < Group fields or expressions >
HAVING < Group filter criteria >
WITH ROLLUP
ORDER BY < Sort field or expression >
LIMIT [m,]n
Wrong writing order SELECT The syntax error will be prompted when the statement is executed ( Error code :1064).
Two 、SELECT The order in which statements are executed
SELECT The execution order of each component of the statement is as follows :
--(1) Specify the table used by the query
FROM <table_name> [INNER|LEFT|RIGHT] JOIN <table_name>
ON < Connection condition >
--(2) Specify filter criteria (WHERE Can't be used later SELECT Alias of the specified column , Because at this time select Clause has not been executed )
WHERE < filter >
--(3) Grouping data
GROUP BY < Group fields or expressions >
WITH ROLLUP
HAVING < Group filter criteria >
--(4) Generate the columns and expressions that need to be displayed
SELECT < List of fields or expressions >
--(5) De duplicate data rows
DISTINCT
--(6) Sort query results
ORDER BY < Sort field or expression >
LIMIT [m,]n
Instructions for alias names of columns and expressions :
(1)WHERE Clause cannot use aliases for columns and expressions .
(2)GROUP BY and ORDER BY Clause can use aliases for columns and expressions .
3、 ... and 、 stay SELECT Use variables in statements
1、 stay FROM Assign values to variables in Clause
Throughout the query process ,FROM Clause is executed only once , therefore FROM The operation of assigning a value to a variable in the clause is performed only once .
Verified as follows :
/* SELECT @var := 0; SELECT s.* FROM student s,(SELECT @var := @var + 1) a; SELECT @var; */
mysql> SELECT @var := 0;
+-----------+
| @var := 0 |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT s.* FROM student s,(SELECT @var := @var + 1) a;
+-------+-----------+--------+---------------------+-------------+-----------+
| s_id | s_name | gender | birth | phone | addr |
+-------+-----------+--------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | male | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2012 | Liu Xiaoqing | Woman | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2014 | Liu Yan | Woman | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2015 | Liu Yan | Woman | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2016 | Liu Ruofei | Woman | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
| S2021 | Dong Wenhua | Woman | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2024 | Obama | male | 2000-10-17 00:00:00 | 13843735885 | Xinyang City |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
| S2031 | Lee Myung Bak | Woman | 1999-10-26 00:00:00 | 13643732222 | Zhengzhou city |
| S2032 | Vinci | male | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city |
+-------+-----------+--------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)
mysql> SELECT @var;
+------+
| @var |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
2、 stay SELECT Assign values to variables in Clause
Throughout the query process ,SELECT The number of times the clause is executed depends on the number of rows recorded in the query result . therefore , How many rows are there in the query result , be SELECT The operation of assigning values to variables in the clause is performed several times . According to this characteristic , have access to COUNT(1) Count the number of rows in the query result .
Verified as follows :
/* SELECT s.*,@var := @var + 1 FROM student s,(SELECT @var := 0) a; SELECT @var; */
mysql> SELECT s.*,@var := @var + 1 FROM student s,(SELECT @var := 0) a;
+-------+-----------+--------+---------------------+-------------+-----------+------------------+
| s_id | s_name | gender | birth | phone | addr | @var := @var + 1 |
+-------+-----------+--------+---------------------+-------------+-----------+------------------+
| S2011 | Zhang Xiaogang | male | 1999-12-03 00:00:00 | 13163735775 | Xinyang City | 1 |
| S2012 | Liu Xiaoqing | Woman | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City | 2 |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city | 3 |
| S2014 | Liu Yan | Woman | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city | 4 |
| S2015 | Liu Yan | Woman | 1999-07-06 00:00:00 | 13813735225 | Xinyang City | 5 |
| S2016 | Liu Ruofei | Woman | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City | 6 |
| S2021 | Dong Wenhua | Woman | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City | 7 |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city | 8 |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City | 9 |
| S2024 | Obama | male | 2000-10-17 00:00:00 | 13843735885 | Xinyang City | 10 |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City | 11 |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city | 12 |
| S2031 | Lee Myung Bak | Woman | 1999-10-26 00:00:00 | 13643732222 | Zhengzhou city | 13 |
| S2032 | Vinci | male | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city | 14 |
+-------+-----------+--------+---------------------+-------------+-----------+------------------+
14 rows in set (0.00 sec)
mysql> SELECT @var;
+------+
| @var |
+------+
| 14 |
+------+
1 row in set (0.00 sec)
3、 Count the number of rows in the query result
mysql> select count(1) from student where addr=' Zhengzhou city ';
+----------+
| count(1) |
+----------+
| 6 |
+----------+
1 row in set (0.04 sec)
mysql> select sum(1) from student where addr=' Zhengzhou city ';
+--------+
| sum(1) |
+--------+
| 6 |
+--------+
1 row in set (0.02 sec)
Four 、 Alias usage rules for columns
1、WHERE Clause cannot use aliases for columns and expressions
/* select s_id,s_name,birth,year(now())-year(birth) age from student where age > 20; */
-- error : Alias not recognized age
mysql> select s_id,s_name,birth,year(now())-year(birth) age
-> from student where age > 20;
ERROR 1054 (42S22): Unknown column 'age' in 'where clause'
2、GROUP BY and ORDER BY Clause can use aliases for columns and expressions
(1)GROUP BY Option use alias
/* select year(now())-year(birth) age, count(*) from student group by age; */
mysql> select year(now())-year(birth) age, count(*)
-> from student group by age;
+------+----------+
| age | count(*) |
+------+----------+
| 22 | 4 |
| 23 | 7 |
| 24 | 3 |
+------+----------+
3 rows in set (0.00 sec)
(2)ORDER BY Option use alias
/* select s_id,s_name,birth,year(now())-year(birth) age from student order by age desc; */
mysql> select s_id,s_name,birth,year(now())-year(birth) age
-> from student order by age desc;
+-------+-----------+---------------------+------+
| s_id | s_name | birth | age |
+-------+-----------+---------------------+------+
| S2013 | Cao mengde | 1998-02-13 00:00:00 | 24 |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 24 |
| S2026 | Zhang Xueyou | 1998-07-06 00:00:00 | 24 |
| S2011 | Zhang Xiaogang | 1999-12-03 00:00:00 | 23 |
| S2012 | Liu Xiaoqing | 1999-10-11 00:00:00 | 23 |
| S2015 | Liu Yan | 1999-07-06 00:00:00 | 23 |
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 23 |
| S2023 | trump | 1999-06-21 00:00:00 | 23 |
| S2031 | Lee Myung Bak | 1999-10-26 00:00:00 | 23 |
| S2032 | Vinci | 1999-12-31 00:00:00 | 23 |
| S2016 | Liu Ruofei | 2000-08-31 00:00:00 | 22 |
| S2021 | Dong Wenhua | 2000-07-30 00:00:00 | 22 |
| S2024 | Obama | 2000-10-17 00:00:00 | 22 |
| S2025 | Zhou Jianhua | 2000-08-22 00:00:00 | 22 |
+-------+-----------+---------------------+------+
14 rows in set (0.00 sec)
5、 ... and 、WHERE and GROUP BY … HAVING Clause
When WHERE and GROUP BY … HAVING When clauses are used at the same time , The execution process is as follows :
(1) perform WHERE Clause to filter the records in the table , Get the row that meets the given condition .
(2) Group the records that meet the query conditions , And then use HAVING Clause to filter groups .
for example : according to student The table queries the addresses 【 male 】 Number of students , And display more than 2 Human behavior .
The order is as follows :
/* select addr,count(*) cnt from student where gender = ' male ' group by addr having cnt>2; */
mysql> select addr,count(*) cnt from student
-> where gender = ' male '
-> group by addr having cnt>2;
+-----------+-----+
| addr | cnt |
+-----------+-----+
| Zhengzhou city | 4 |
+-----------+-----+
1 row in set (0.00 sec)
The above query can be executed step by step :
(1) Inquire about student In the table 【 male 】 Health information
mysql> select * from student where gender = ' male ';
+-------+-----------+--------+---------------------+-------------+-----------+
| s_id | s_name | gender | birth | phone | addr |
+-------+-----------+--------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | male | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2013 | Cao mengde | male | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2022 | Zhou Huajian | male | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | male | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2024 | Obama | male | 2000-10-17 00:00:00 | 13843735885 | Xinyang City |
| S2025 | Zhou Jianhua | male | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2026 | Zhang Xueyou | male | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
| S2032 | Vinci | male | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city |
+-------+-----------+--------+---------------------+-------------+-----------+
8 rows in set (0.00 sec)
(2) Press addr Grouping , Count the number of students in each address
/* select addr, count(*) cnt from student where gender = ' male ' group by addr; */
mysql> select addr, count(*) cnt from student where gender = ' male '
-> group by addr;
+-----------+-----+
| addr | cnt |
+-----------+-----+
| Xinyang City | 2 |
| Kaifeng City | 1 |
| Xinxiang City | 1 |
| Zhengzhou city | 4 |
+-----------+-----+
4 rows in set (0.00 sec)
(3) Use HAVING Clause to filter groups
/* select addr, count(*) cnt from student where gender = ' male ' group by addr having cnt > 2; */
mysql> select addr, count(*) cnt from student where gender = ' male '
-> group by addr having cnt > 2;
+-----------+-----+
| addr | cnt |
+-----------+-----+
| Zhengzhou city | 4 |
+-----------+-----+
1 row in set (0.00 sec)
边栏推荐
- Reverse linked list drawing demonstration
- 北峰通信亮相中国(厦门)应急展|智能化通信手段强势吸睛!
- MariaDB database upgrade version
- Introduction to QT (2.1 the first procedure for the beginning of QT)
- Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
- Creo 9.0 mouse button operation for model observation
- Accelerating matrix vector multiplication of special matrices with FFT
- 工作3年的测试员跳槽后工资是原来的2倍,秘诀原来是......
- C language: deep analysis of const keyword
- Résumé du websocket minier
猜你喜欢

QT入门篇(2.1初入QT的开始第一个程序)

Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P

How to use mitmproxy to get data return in automated testing?

【电赛训练】非接触物体尺寸形态测量 2020年电赛G题

How to realize 485 wireless communication between multiple sensors and Siemens PLC?

postman测试接口在URL配置正确的情况下出现404或者500错误

Distributed cap principle

Method of C language annotation

Focus on microservices

How can dbcontext support the migration of different databases in efcore advanced SaaS system
随机推荐
Bean Validation使用篇----05
Xilinx FPGA one way clock input two PLLs
MySQL table field quantity limit and row size limit
JS determines whether the element scrolls to the top
Image processing 1:rgb888_ YCbCr444
Problem note - unable to open include file: "direct.h": no such file or directory
MySQL common commands
Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
Expérience du système réseau: résoudre les problèmes de ping
postman测试接口在URL配置正确的情况下出现404或者500错误
Detailed explanation of data warehouse standard -2022
Leetcode set the intersection size to at least 2
How to use mitmproxy to get data return in automated testing?
Customize an object
MariaDB database upgrade version
vim常用命令
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
C language writing specification
[data mining engineer - written examination] Haier company in 2022
Introduction to several scenarios involving programming operation of Excel in SAP implementation project