当前位置:网站首页>Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
2022-07-24 00:49:00 【Rsda DBA_ WGX】
Database system principle and Application Tutorial (046)—— MySQL Inquire about ( 8、 ... and ): Group query (GROUP BY)
Use GROUP BY Keywords can group query results by one or more columns or expressions , Grouping is based on GROUP BY The following column name or expression .GROUP BY Usually used with aggregate functions .
One 、GROUP BY Usage of
GROUP BY The syntax of the clause is as follows :
GROUP BY < Name | expression >[,...] [HAVING Conditional expression ] [WITH ROLLUP]
/* explain : (1) When using grouped queries ,select The following field list can only contain GROUP BY The following column names or expressions and aggregate functions , Cannot contain other columns or expressions , Otherwise, an error will be reported . (2) Name | expression : Group by , Group by column name or expression . (3)HAVING Conditional expression : Select groups , The result of the conditional expression will be displayed . (4)WITH ROLLUP: Add a record at the end of all records , This record is the statistical result of all rows . (5) have access to GROUP_CONCAT() The function concatenates all the values in a field into a string . */
for example :
(1) Query the number of students corresponding to each address .
mysql> select addr,count(*) from student group by addr;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Xinyang City | 3 |
| Kaifeng City | 3 |
| Xinxiang City | 2 |
| Zhengzhou city | 6 |
+-----------+----------+
4 rows in set (0.00 sec)
-- The columns included in this query s_name Neither belong to group by Not in the aggregate function , So there was an error .
mysql> select s_name,addr,count(*) from student group by addr;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mydb.student.s_name' which is not functionally dependent on co
lumns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
(2) Check the list of students corresponding to each address .
mysql> select addr,group_concat(s_name) from student group by addr;
+-----------+----------------------------------------------------------+
| addr | group_concat(s_name) |
+-----------+----------------------------------------------------------+
| Xinyang City | Zhang Xiaogang , Liu Yan , Obama |
| Kaifeng City | Liu Ruofei , Dong Wenhua , Zhou Jianhua |
| Xinxiang City | Liu Xiaoqing , trump |
| Zhengzhou city | Cao mengde , Liu Yan , Zhou Huajian , Zhang Xueyou , Lee Myung Bak , Vinci |
+-----------+----------------------------------------------------------+
4 rows in set (0.00 sec)
Two 、 Use expressions to group
for example : Group by the first three digits of the mobile phone number , Query the number of students in each group .
mysql> select left(phone,3) phone3,count(*) from student group by 1;
+--------+----------+
| phone3 | count(*) |
+--------+----------+
| 130 | 1 |
| 131 | 1 |
| 132 | 1 |
| 133 | 1 |
| 135 | 1 |
| 136 | 4 |
| 137 | 2 |
| 138 | 3 |
+--------+----------+
8 rows in set (0.00 sec)
3、 ... and 、 Use multiple columns to group
for example : according to addr and gender grouping , Query the number of records in each group .
mysql> select * from student;
+-------+-----------+--------+---------------------+-------------+-----------+
| 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 addr,gender,count(*) from student group by 1,2;
+-----------+--------+----------+
| addr | gender | count(*) |
+-----------+--------+----------+
| Xinyang City | Woman | 1 |
| Xinyang City | male | 2 |
| Kaifeng City | Woman | 2 |
| Kaifeng City | male | 1 |
| Xinxiang City | Woman | 1 |
| Xinxiang City | male | 1 |
| Zhengzhou city | Woman | 2 |
| Zhengzhou city | male | 4 |
+-----------+--------+----------+
8 rows in set (0.00 sec)
Four 、 When grouping, use WHERE Clause
If WHERE Clause , First use WHERE Filter the data in the table , Then group and count .
for example : Query the address corresponding to each address in the student table 【 schoolboy 】 Number .
mysql> select addr,count(*) from student where gender = ' male ' group by addr;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Xinyang City | 2 |
| Kaifeng City | 1 |
| Xinxiang City | 1 |
| Zhengzhou city | 4 |
+-----------+----------+
4 rows in set (0.00 sec)
5、 ... and 、 Use HAVING Select groups
Use HAVING Clause allows you to select groups . When HAVING Clause and WHER When clauses are used at the same time , The order of query execution is : First use WHERE Filter the records in the table , Then group and count the records that meet the conditions , Reuse HAVING Clause to select groups .
for example :
(1) according to addr grouping , Query the number of people corresponding to each address .
mysql> select addr,count(*) from student group by addr;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Xinyang City | 3 |
| Kaifeng City | 3 |
| Xinxiang City | 2 |
| Zhengzhou city | 6 |
+-----------+----------+
4 rows in set (0.00 sec)
(2) according to addr grouping , The number of people in each address exceeds 3 Group of people .
mysql> select addr,count(*) from student group by addr having count(*)>3;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Zhengzhou city | 6 |
+-----------+----------+
1 row in set (0.00 sec)
(3) according to addr grouping , The number of boys in each address exceeds 3 Group of people .
mysql> select addr,count(*) from student where gender = ' male '
-> group by addr having count(*)>3;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Zhengzhou city | 4 |
+-----------+----------+
1 row in set (0.00 sec)
-- The execution process of this query is as follows :
-- 1、 The screening gender is 【 male 】 Student records of
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、 in the light of addr grouping
mysql> select addr,count(*) from student where gender = ' male ' group by addr;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Xinyang City | 2 |
| Kaifeng City | 1 |
| Xinxiang City | 1 |
| Zhengzhou city | 4 |
+-----------+----------+
4 rows in set (0.00 sec)
-- 3、 Filter the groups
mysql> select addr,count(*) from student
where gender = ' male ' group by addr having count(*)>3;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Zhengzhou city | 4 |
+-----------+----------+
1 row in set (0.00 sec)
6、 ... and 、WITH ROLLUP Parameters
Add a record at the end of all records , This record is the statistical result of all rows .
for example : according to addr grouping , Query the number of people corresponding to each address .
mysql> select addr,count(*) from student group by addr with rollup;
+-----------+----------+
| addr | count(*) |
+-----------+----------+
| Xinyang City | 3 |
| Kaifeng City | 3 |
| Xinxiang City | 2 |
| Zhengzhou city | 6 |
| NULL | 14 |
+-----------+----------+
5 rows in set (0.00 sec)
边栏推荐
- What is the function of the select... For UPDATE statement? Can you lock tables or rows?
- Image processing: Generation 3 × Window of 3
- MariaDB database upgrade version
- 数据标准详细概述-2022
- Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
- MySQL exercise: all employees reporting to the CEO
- Unity metaverse (I). Ready player me & blender customize your Avatar
- 采坑websocket总结
- Bert article translation
- A good habit to develop when writing SQL
猜你喜欢

How to use SAP intelligent robotic process automation to automate Excel

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘

Case error of MySQL branch statement

Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?

Redis master-slave synchronization mechanism

Docker pulls the redis image and runs it

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

如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel

Fpga:ov7725 camera displays images in rgb565 format through vga/hdmi
随机推荐
How to use mitmproxy to get data return in automated testing?
Coloring old photos - deoldify get started quickly
Detailed explanation of data warehouse standard -2022
Creo 9.0 mouse button operation for model observation
MariaDB database upgrade version
《天幕红尘》笔记与思考(六)因缺而需
[data mining engineer - written examination] Haier company in 2022
Treatment of particle boundary collision
[low code] limitations of low code development
Difference between data index and label system of data warehouse
High number_ Chapter 1 space analytic geometry and vector algebra__ Two point distance
AWS Article 3 how to publish message to IOT mqtt in go language
Flutter | the easiest way to add header and footer to listview
Educational Codeforces Round 132 (Rated for Div. 2)(A-D)
網絡系統實驗:ping不通的問題解决
MySQL exercise: all employees reporting to the CEO
Redis distributed lock to be continued
How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
Overview of data model design method
Leetcode set the intersection size to at least 2