当前位置:网站首页>Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
2022-07-24 00:48:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (039)—— MySQL Inquire about ( One ):SELECT Syntax analysis of commands
Catalog
- Database system principle and Application Tutorial (039)—— MySQL Inquire about ( One ):SELECT Syntax analysis of commands
MySQL Use SELECT Statement to query the required data . Query refers to the use of demand , Use different query methods to obtain the required data from the database , Query is MySQL The most important operation .
One 、SELECT The grammatical structure of a sentence
SELECT The syntax format of the statement is as follows :
SELECT DISTINCT
< Column name or expression list >
FROM
< Table name > < Connection type >
JOIN < Table name > ON < Connection condition >
WHERE
< filter >
GROUP BY
< Group field list >
HAVING
< Group filter criteria >
ORDER BY
< Sort field >
LIMIT <m,n>;
/* Parameter description : (1)DISTINCT: Eliminate duplicate lines . (2)< List of fields or expressions >: Indicates the name of the field to be queried , have access to (*) Represents all fields , You can also use column names to construct expressions . (3)< Connection type >: have access to inner join,left join,right join Respectively represents internal connection , Left connection , The right connection . (4)< Connection condition >: Generally, the same columns in two tables are used to construct connection conditions . Tables that perform join operations usually have one to many connections , Connection conditions are usually established by using the foreign key of the child table and the primary key of the parent table . (5)< filter >: The restricted query data must meet the query condition . (6)< Group field list >: Group according to the specified fields . (7)< Group filter criteria >: Filter the groups . (8)< Sort field >: Sort query results , Can be in ascending order (ASC) And descending (DESC) array , The default is ascending . (9)LIMIT <m,n>: Display data in pages . */
Two 、 Prepare the data table required for query
/* CREATE TABLE student( s_id char(5) primary key, s_name char(20), birth datetime, phone char(20), addr varchar(100) ); INSERT INTO student VALUES('S2011',' Zhang Xiaogang ','1999-12-3','13163735775',' Xinyang City '), ('S2012',' Liu Xiaoqing ','1999-10-11','13603732255',' Xinxiang City '), ('S2013',' Cao mengde ','1998-2-13','13853735522',' Zhengzhou city '), ('S2014',' Liu Yan ','1998-6-24','13623735335',' Zhengzhou city '), ('S2015',' Liu Yan ','1999-7-6','13813735225',' Xinyang City '), ('S2016',' Liu Ruofei ','2000-8-31','13683735533',' Kaifeng City '), ('S2021',' Dong Wenhua ','2000-7-30','13533735564',' Kaifeng City '), ('S2022',' Zhou Huajian ','1999-5-25','13243735578',' Zhengzhou city '), ('S2023',' trump ','1999-6-21','13343735588',' Xinxiang City '), ('S2024',' Obama ','2000-10-17','13843735885',' Xinyang City '), ('S2025',' Zhou Jianhua ','2000-8-22','13788736655',' Kaifeng City '), ('S2026',' Zhang Xueyou ','1998-7-6','13743735566',' Zhengzhou city '), ('S2031',' Lee Myung Bak ','1999-10-26','13643732222',' Zhengzhou city '), ('S2032',' Vinci ','1999-12-31','13043731234',' Zhengzhou city '); CREATE TABLE teacher( t_id char(5) primary key, t_name char(20), job_title char(20), phone char(20) ); INSERT INTO teacher VALUES('T8001',' Ouyang xiu ',' professor ','13703735666'), ('T8002',' Hua Luogeng ',' professor ','13703735888'), ('T8003',' Zhong Nanshan ',' professor ','13703735675'), ('T8004',' Qian xuesen ',' professor ','13703735638'), ('T8005',' Li Bai ',' associate professor ','13703735828'), ('T8006',' Confucius ',' professor ','13703735457'), ('T8007',' Wang Anshi ',' associate professor ','13703735369'); CREATE TABLE course( c_id char(4) primary key, c_name char(20), t_id char(5) ); INSERT INTO course VALUES('C101',' Ancient literature ','T8001'), ('C102',' Advanced mathematics ','T8002'), ('C103',' linear algebra ','T8002'), ('C104',' Clinical medicine ','T8003'), ('C105',' Infectious diseases ','T8003'), ('C106',' College Physics ','T8004'), ('C107',' Poetry appreciation ','T8005'), ('C108',' pedagogy ','T8006'), ('C109',' criminal procedure law ','T8007'), ('C110',' economic law ','T8007'); CREATE TABLE score( s_id char(5), c_id char(4), score int, primary key(s_id, c_id) ); INSERT INTO score VALUES('S2011','C102',84),('S2011','C105',90),('S2011','C106',79),('S2011','C109',65), ('S2012','C101',67),('S2012','C102',52),('S2012','C103',55),('S2012','C104',86), ('S2012','C105',87),('S2012','C106',64),('S2012','C107',62), ('S2012','C108',73),('S2012','C109',78),('S2012','C110',89), ('S2013','C102',97),('S2013','C103',68),('S2013','C104',66),('S2013','C105',68), ('S2014','C102',90),('S2014','C103',85),('S2014','C104',77),('S2014','C105',96), ('S2015','C101',69),('S2015','C102',66),('S2015','C103',88),('S2015','C104',69), ('S2015','C105',66),('S2015','C106',88),('S2015','C107',69), ('S2015','C108',66),('S2015','C109',88),('S2015','C110',69), ('S2016','C101',65),('S2016','C102',69),('S2016','C107',82),('S2016','C108',56), ('S2021','C102',72),('S2021','C103',90),('S2021','C104',90),('S2021','C105',57), ('S2022','C102',88),('S2022','C103',93),('S2022','C109',47),('S2022','C110',62), ('S2023','C102',68),('S2023','C103',86),('S2023','C109',56),('S2023','C110',91), ('S2024','C102',87),('S2024','C103',97),('S2024','C109',80),('S2024','C110',81), ('S2025','C102',61),('S2025','C105',62),('S2025','C106',87),('S2025','C109',82), ('S2026','C102',59),('S2026','C105',48),('S2026','C106',90),('S2026','C109',73); */
mysql> select * from student;
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2012 | Liu Xiaoqing | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2013 | Cao mengde | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2015 | Liu Yan | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2016 | Liu Ruofei | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
| S2021 | Dong Wenhua | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City |
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2024 | Obama | 2000-10-17 00:00:00 | 13843735885 | Xinyang City |
| S2025 | Zhou Jianhua | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2026 | Zhang Xueyou | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
| S2031 | Lee Myung Bak | 1999-10-26 00:00:00 | 13643732222 | Zhengzhou city |
| S2032 | Vinci | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)
mysql> select * from teacher;
+-------+-----------+-----------+-------------+
| t_id | t_name | job_title | phone |
+-------+-----------+-----------+-------------+
| T8001 | Ouyang xiu | professor | 13703735666 |
| T8002 | Hua Luogeng | professor | 13703735888 |
| T8003 | Zhong Nanshan | professor | 13703735675 |
| T8004 | Qian xuesen | professor | 13703735638 |
| T8005 | Li Bai | associate professor | 13703735828 |
| T8006 | Confucius | professor | 13703735457 |
| T8007 | Wang Anshi | associate professor | 13703735369 |
+-------+-----------+-----------+-------------+
7 rows in set (0.00 sec)
mysql> select * from course;
+------+-----------------+-------+
| c_id | c_name | t_id |
+------+-----------------+-------+
| C101 | Ancient literature | T8001 |
| C102 | Advanced mathematics | T8002 |
| C103 | linear algebra | T8002 |
| C104 | Clinical medicine | T8003 |
| C105 | Infectious diseases | T8003 |
| C106 | College Physics | T8004 |
| C107 | Poetry appreciation | T8005 |
| C108 | pedagogy | T8006 |
| C109 | criminal procedure law | T8007 |
| C110 | economic law | T8007 |
+------+-----------------+-------+
10 rows in set (0.00 sec)
mysql> select * from score;
+-------+------+-------+
| s_id | c_id | score |
+-------+------+-------+
| S2011 | C102 | 84 |
| S2011 | C105 | 90 |
| S2011 | C106 | 79 |
| S2011 | C109 | 65 |
| S2012 | C101 | 67 |
| S2012 | C102 | 52 |
| S2012 | C103 | 55 |
| S2012 | C104 | 86 |
| S2012 | C105 | 87 |
| S2012 | C106 | 64 |
| S2012 | C107 | 62 |
| S2012 | C108 | 73 |
| S2012 | C109 | 78 |
| S2012 | C110 | 89 |
| S2013 | C102 | 97 |
| S2013 | C103 | 68 |
| S2013 | C104 | 66 |
| S2013 | C105 | 68 |
| S2014 | C102 | 90 |
| S2014 | C103 | 85 |
| S2014 | C104 | 77 |
| S2014 | C105 | 96 |
| S2015 | C101 | 69 |
| S2015 | C102 | 66 |
| S2015 | C103 | 88 |
| S2015 | C104 | 69 |
| S2015 | C105 | 66 |
| S2015 | C106 | 88 |
| S2015 | C107 | 69 |
| S2015 | C108 | 66 |
| S2015 | C109 | 88 |
| S2015 | C110 | 69 |
| S2016 | C101 | 65 |
| S2016 | C102 | 69 |
| S2016 | C107 | 82 |
| S2016 | C108 | 56 |
| S2021 | C102 | 72 |
| S2021 | C103 | 90 |
| S2021 | C104 | 90 |
| S2021 | C105 | 57 |
| S2022 | C102 | 88 |
| S2022 | C103 | 93 |
| S2022 | C109 | 47 |
| S2022 | C110 | 62 |
| S2023 | C102 | 68 |
| S2023 | C103 | 86 |
| S2023 | C109 | 56 |
| S2023 | C110 | 91 |
| S2024 | C102 | 87 |
| S2024 | C103 | 97 |
| S2024 | C109 | 80 |
| S2024 | C110 | 81 |
| S2025 | C102 | 61 |
| S2025 | C105 | 62 |
| S2025 | C106 | 87 |
| S2025 | C109 | 82 |
| S2026 | C102 | 59 |
| S2026 | C105 | 48 |
| S2026 | C106 | 90 |
| S2026 | C109 | 73 |
+-------+------+-------+
60 rows in set (0.00 sec)
3、 ... and 、SELECT The writing order and execution order of statements
1、SELECT The writing order of sentences
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
If the writing order is wrong, it will prompt the wrong writing (1064):
-- order by and where Wrong order
mysql> select * from student order by s_name where addr=' Zhengzhou city ';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where addr=' zhengzhou
City '' at line 1
-- where and from Wrong order
mysql> select * where addr=' Zhengzhou city ' from student;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where addr=' zhengzhou
City ' from student' at line 1
-- where and group Wrong order
mysql> select addr,count(*) from student group by addr where s_name like ' Zhang %';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where s_name like ' Zhang %'' at line 1
2、SELECT The order in which statements are executed
--(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
Four 、 Query example
1、 Basic usage
mysql> select s_id,s_name,year(now())-year(birth) age from student;
+-------+-----------+------+
| s_id | s_name | age |
+-------+-----------+------+
| S2011 | Zhang Xiaogang | 23 |
| S2012 | Liu Xiaoqing | 23 |
| S2013 | Cao mengde | 24 |
| S2014 | Liu Yan | 24 |
| S2015 | Liu Yan | 23 |
| S2016 | Liu Ruofei | 22 |
| S2021 | Dong Wenhua | 22 |
| S2022 | Zhou Huajian | 23 |
| S2023 | trump | 23 |
| S2024 | Obama | 22 |
| S2025 | Zhou Jianhua | 22 |
| S2026 | Zhang Xueyou | 24 |
| S2031 | Lee Myung Bak | 23 |
| S2032 | Vinci | 23 |
+-------+-----------+------+
14 rows in set (0.03 sec)
2、 Specify query criteria
mysql> select * from student where s_name like ' Zhang %';
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2011 | Zhang Xiaogang | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2026 | Zhang Xueyou | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
+-------+-----------+---------------------+-------------+-----------+
2 rows in set (0.02 sec)
3、 Link query
/* select s.s_id, s_name, c.c_id, c_name, score from student s join score sc on s.s_id = sc.s_id join course c on sc.c_id = c.c_id where score > 90; */
mysql> select s.s_id, s_name, c.c_id, c_name, score
-> from student s join score sc
-> on s.s_id = sc.s_id
-> join course c
-> on sc.c_id = c.c_id
-> where score > 90;
+-------+-----------+------+--------------+-------+
| s_id | s_name | c_id | c_name | score |
+-------+-----------+------+--------------+-------+
| S2013 | Cao mengde | C102 | Advanced mathematics | 97 |
| S2014 | Liu Yan | C105 | Infectious diseases | 96 |
| S2022 | Zhou Huajian | C103 | linear algebra | 93 |
| S2023 | trump | C110 | economic law | 91 |
| S2024 | Obama | C103 | linear algebra | 97 |
+-------+-----------+------+--------------+-------+
5 rows in set (0.00 sec)
4、 Sort query results
mysql> select * from student order by birth desc;
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2024 | Obama | 2000-10-17 00:00:00 | 13843735885 | Xinyang City |
| S2016 | Liu Ruofei | 2000-08-31 00:00:00 | 13683735533 | Kaifeng City |
| S2025 | Zhou Jianhua | 2000-08-22 00:00:00 | 13788736655 | Kaifeng City |
| S2021 | Dong Wenhua | 2000-07-30 00:00:00 | 13533735564 | Kaifeng City |
| S2032 | Vinci | 1999-12-31 00:00:00 | 13043731234 | Zhengzhou city |
| S2011 | Zhang Xiaogang | 1999-12-03 00:00:00 | 13163735775 | Xinyang City |
| S2031 | Lee Myung Bak | 1999-10-26 00:00:00 | 13643732222 | Zhengzhou city |
| S2012 | Liu Xiaoqing | 1999-10-11 00:00:00 | 13603732255 | Xinxiang City |
| S2015 | Liu Yan | 1999-07-06 00:00:00 | 13813735225 | Xinyang City |
| S2023 | trump | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2026 | Zhang Xueyou | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2013 | Cao mengde | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)
5、 Group query
mysql> select addr,count(*) cnt from student group by addr;
+-----------+-----+
| addr | cnt |
+-----------+-----+
| Xinyang City | 3 |
| Kaifeng City | 3 |
| Xinxiang City | 2 |
| Zhengzhou city | 6 |
+-----------+-----+
4 rows in set (0.00 sec)
-- Filter the groups
mysql> select addr,count(*) cnt from student group by addr having cnt>2;
+-----------+-----+
| addr | cnt |
+-----------+-----+
| Xinyang City | 3 |
| Kaifeng City | 3 |
| Zhengzhou city | 6 |
+-----------+-----+
3 rows in set (0.00 sec)
6、 Paging query
-- Take the first three records
mysql> select * from student order by birth limit 3;
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2013 | Cao mengde | 1998-02-13 00:00:00 | 13853735522 | Zhengzhou city |
| S2014 | Liu Yan | 1998-06-24 00:00:00 | 13623735335 | Zhengzhou city |
| S2026 | Zhang Xueyou | 1998-07-06 00:00:00 | 13743735566 | Zhengzhou city |
+-------+-----------+---------------------+-------------+-----------+
3 rows in set (0.00 sec)
-- From 4 Bar start , take 2 Bar record
mysql> select * from student order by birth limit 3,2;
+-------+-----------+---------------------+-------------+-----------+
| s_id | s_name | birth | phone | addr |
+-------+-----------+---------------------+-------------+-----------+
| S2022 | Zhou Huajian | 1999-05-25 00:00:00 | 13243735578 | Zhengzhou city |
| S2023 | trump | 1999-06-21 00:00:00 | 13343735588 | Xinxiang City |
+-------+-----------+---------------------+-------------+-----------+
2 rows in set (0.00 sec)
边栏推荐
- MySQL exercise: all employees reporting to the CEO
- Difference between data index and label system of data warehouse
- postman测试接口在URL配置正确的情况下出现404或者500错误
- 网络系统实验:ping不通的问题解决
- The postman test interface has 404 or 500 errors when the URL is configured correctly
- How to use mitmproxy to get data return in automated testing?
- 網絡系統實驗:ping不通的問題解决
- Flutter | firstwhere error reporting
- The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
- XXL job realizes the code parsing of email sending warnings (line by line code interpretation)
猜你喜欢

A good habit to develop when writing SQL

Starfish OS: create a new paradigm of the meta universe with reality as the link

测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)

数据标准详细概述-2022

mysql 分支语句case报错

Don't let Fujin Yibo see this

Docker pulls the redis image and runs it

Detailed overview of data standards -2022

Bean validation usage article ----05

Redis cluster hash sharding algorithm (slot location algorithm)
随机推荐
Gbase 8C mode visibility query function (2)
Classic example of C language - convert the input two digits into English
Small farmers also have big goals in the test, and the latest big bat interview summary (constantly updating...)
Summary of the fourth week of summer vacation
测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
Codeforces Round #807 (Div. 2)(A-D)
How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
How to improve data quality
Difference between data index and label system of data warehouse
Application of SCA on devsecops platform
SAP 实施项目中涉及到编程方式操作 Excel 的几种场景介绍
【Flyway 介绍】
Summary of polynomial commitment schemes
Detailed overview of data standards -2022
The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
Table custom table encapsulation
Gbase 8C system table information function (II)
Accelerating matrix vector multiplication of special matrices with FFT
C language writing specification
Gbase 8C access authority query function (6)