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

Vue sample code for online preview of office files

I'm working on electronic archives recently, ...

How to encapsulate WangEditor rich text component in Angular

The rich text component is a very commonly used c...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

Implementation of grayscale release with Nginx and Lua

Install memcached yum install -y memcached #Start...

Detailed explanation of GaussDB for MySQL performance optimization

Table of contents background Inspiration comes fr...

Solution to Vue data assignment problem

Let me summarize a problem that I have encountere...

How to pass parameters to JS via CSS

1. Background that needs to be passed through CSS...

Linux CentOS6.5 yum install mysql5.6

This article shares the simple process of install...

Vue calls the computer camera to realize the photo function

This article example shares the specific code of ...

How to build a new image based on an existing image in Docker

Building new images from existing images is done ...