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
MySQL Limit can query database data in segments a...
I'm working on electronic archives recently, ...
The rich text component is a very commonly used c...
Execute the create table statement in the databas...
The question is referenced from: https://www.zhih...
As a front-end monkey, whether it is during an in...
This article shows you how to use CSS to create a...
Install memcached yum install -y memcached #Start...
Table of contents background Inspiration comes fr...
Let me summarize a problem that I have encountere...
1. Background that needs to be passed through CSS...
This article shares the simple process of install...
After installing a centos8 service under vmware a...
This article example shares the specific code of ...
Building new images from existing images is done ...