mysql group by grouping multiple fields

mysql group by grouping multiple fields

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:

Table: Subject_Selection

Subject Semester Attendee
---------------------------------
ITB001 1 John
ITB001 1 Bob
ITB001 1 Mickey
ITB001 2 Jenny
ITB001 2 James
MKB114 1 John
MKB114 1 Erica

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:

Subject Count
------------------------------
ITB001 5
MKB114 2

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:

Subject Semester Count
------------------------------
ITB001 1 3
ITB001 2 2
MKB114 1 2

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:
  • MySQL merges multiple rows of data based on the group_concat() function
  • mysql group_concat method example to write group fields into one row
  • MySql Group By implements grouping of multiple fields
  • How to merge and display multiple data of a field after grouping in MySQL

<<:  Source code reveals why Vue2 this can directly obtain data and methods

>>:  Detailed steps for deepin20 to install NVIDIA closed-source drivers

Recommend

Methods and steps for deploying go projects based on Docker images

Dependence on knowledge Go cross-compilation basi...

...

How to deploy zabbix_agent in docker

zabbix_agent deployment: Recommendation: zabbix_a...

Vue3.0 implements the encapsulation of the drop-down menu

Vue3.0 has been out for a while, and it is necess...

Linux disk space release problem summary

The /partition utilization of a server in IDC is ...

The most commonly used HTML tags to create web pages

1. Optimization of commonly used HTML tags HTML s...

How to use the Linux nl command

1. Command Introduction nl (Number of Lines) adds...

Analysis of MySQL Aborted connection warning log

Preface: Sometimes, the session connected to MySQ...

Setting up VMware vSphere in VMware Workstation (Graphic Tutorial)

VMware vSphere is the industry's leading and ...

How to use Typescript to encapsulate local storage

Table of contents Preface Local storage usage sce...

Detailed steps for developing Java payment interface for Alipay

Table of contents first step Step 2 Step 3 Step 4...

Basic steps to use Mysql SSH tunnel connection

Preface For security reasons, the root user of My...

How to deploy nginx with Docker and modify the configuration file

Deploy nginx with docker, it's so simple Just...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...