In daily development tasks, we often use MYSQL's GROUP BY grouping to obtain statistical data based on the grouping fields in the data table. For example, there is a student course selection table with the following structure:
We want to count how many students have signed up for each course, and apply the following SQL: SELECT Subject, Count(*) FROM Subject_Selection GROUP BY Subject The following results are obtained:
Because the table records that 5 students chose ITB001 and 2 students chose MKB114. The reasons for this result are: GROUP BY X means putting all records with the same X field value into one group. What about GROUP BY X, Y? GROUP BY X, Y means putting all records with the same X and Y field values into one group. Next, we need to count how many people choose each subject each semester, and apply the following SQL: SELECT Subject, Semester, Count(*) FROM Subject_Selection GROUP BY Subject, Semester The above SQL means to group the data in the Subject_Selection table, put the records with the same Subject and Semester field values into the same group, and then apply aggregate functions (COUNT, SUM, AVG, etc.) to the data in each group. The result is:
From the records in the table, we can see that the grouping result is correct. Three students chose ITB001 in the first semester, two students chose ITB001 in the second semester, and two students chose MKB114 in the first semester. No one chose MKB114 in the second semester. The above article about mysql group by to group multiple fields is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Source code reveals why Vue2 this can directly obtain data and methods
>>: Detailed steps for deepin20 to install NVIDIA closed-source drivers
Dependence on knowledge Go cross-compilation basi...
zabbix_agent deployment: Recommendation: zabbix_a...
Vue3.0 has been out for a while, and it is necess...
The /partition utilization of a server in IDC is ...
1. Optimization of commonly used HTML tags HTML s...
1. Command Introduction nl (Number of Lines) adds...
Preface: Sometimes, the session connected to MySQ...
VMware vSphere is the industry's leading and ...
HTML5 adds a native placeholder attribute for inp...
Table of contents Preface Local storage usage sce...
Table of contents first step Step 2 Step 3 Step 4...
Preface For security reasons, the root user of My...
Deploy nginx with docker, it's so simple Just...
Use the rpm installation package to install mysql...