MySQL group by Explanation of Single Word Grouping Order and Multi field Grouping Method

  • 2021-11-29 16:42:31
  • OfStack

I have created an goods table here. First, look at the data inside:


mysql> select * from goods;
+----+------+------+------------+-------------+------------+
| id | s_id | b_id | goods_name | goods_price | goods_desc |
+----+------+------+------------+-------------+------------+
| 1 |  1 |  5 | book    |    22.35 | book    |
| 2 |  2 |  5 | ball    |    32.25 | ball    |
| 3 |  3 |  5 | NULL    |    3.23 | NULL    |
| 4 |  3 |  5 | macbook  |    3.23 | book    |
| 5 |  3 |  5 | listbook  |    2.30 | book    |
| 6 |  1 |  1 | nicebook  |   9999.00 | nicebook  |
| 7 |  2 |  3 | googlebook |    25.30 | book    |
+----+------+------+------------+-------------+------------+

1. Grouping according to s_id


mysql> select *,group_concat(goods_name) goods_names,group_concat(goods_desc) goods_descs,group_concat(id) ids,group_concat(goods_price) goods_prices from goods group by s_id;
+----+------+------+------------+-------------+------------+------------------+---------------+-------+----------------+
| id | s_id | b_id | goods_name | goods_price | goods_desc | goods_names   | goods_descs  | ids  | goods_prices  |
+----+------+------+------------+-------------+------------+------------------+---------------+-------+----------------+
| 1 |  1 |  5 | book    |    22.35 | book    | book,nicebook  | book,nicebook | 1,6  | 22.35,9999.00 |
| 2 |  2 |  5 | ball    |    32.25 | ball    | ball,googlebook | ball,book   | 2,7  | 32.25,25.30  |
| 3 |  3 |  5 | NULL    |    3.23 | NULL    | macbook,listbook | book,book   | 3,4,5 | 3.23,3.23,2.30 |
+----+------+------+------------+-------------+------------+------------------+---------------+-------+----------------+

Here, we use group_concat() Function, the main purpose is to display the details of the grouping

The above grouping by single field is very simple, and the same s_id All the records are grouped

2. Grouping according to s_id and goods_desc fields

Analysis: When querying grouping here, the data in each group will be grouped according to s_id, and then the data in each group will be grouped according to goods_desc


mysql> select *,group_concat(goods_name) goods_names,group_concat(goods_desc) goods_descs,group_concat(id) ids,group_concat(goods_price) goods_prices from goods group by s_id,goods_desc;
+----+------+------+------------+-------------+------------+------------------+-------------+------+--------------+
| id | s_id | b_id | goods_name | goods_price | goods_desc | goods_names   | goods_descs | ids | goods_prices |
+----+------+------+------------+-------------+------------+------------------+-------------+------+--------------+
| 1 |  1 |  5 | book    |    22.35 | book    | book       | book    | 1  | 22.35    |
| 6 |  1 |  1 | nicebook  |   9999.00 | nicebook  | nicebook     | nicebook  | 6  | 9999.00   |
| 2 |  2 |  5 | ball    |    32.25 | ball    | ball       | ball    | 2  | 32.25    |
| 7 |  2 |  3 | googlebook |    25.30 | book    | googlebook    | book    | 7  | 25.30    |
| 3 |  3 |  5 | NULL    |    3.23 | NULL    | NULL       | NULL    | 3  | 3.23     |
| 4 |  3 |  5 | macbook  |    3.23 | book    | macbook,listbook | book,book  | 4,5 | 3.23,2.30  |
+----+------+------+------------+-------------+------------+------------------+-------------+------+--------------+

Compare goods_descs here with 1 above

Next, you can also group according to goods_price


mysql> select *,group_concat(goods_name) goods_names,group_concat(goods_desc) goods_descs,group_concat(id) ids,group_concat(goods_price) goods_prices from goods group by s_id,goods_desc,goods_price;
+----+------+------+------------+-------------+------------+-------------+-------------+------+--------------+
| id | s_id | b_id | goods_name | goods_price | goods_desc | goods_names | goods_descs | ids | goods_prices |
+----+------+------+------------+-------------+------------+-------------+-------------+------+--------------+
| 1 |  1 |  5 | book    |    22.35 | book    | book    | book    | 1  | 22.35    |
| 6 |  1 |  1 | nicebook  |   9999.00 | nicebook  | nicebook  | nicebook  | 6  | 9999.00   |
| 2 |  2 |  5 | ball    |    32.25 | ball    | ball    | ball    | 2  | 32.25    |
| 7 |  2 |  3 | googlebook |    25.30 | book    | googlebook | book    | 7  | 25.30    |
| 3 |  3 |  5 | NULL    |    3.23 | NULL    | NULL    | NULL    | 3  | 3.23     |
| 5 |  3 |  5 | listbook  |    2.30 | book    | listbook  | book    | 5  | 2.30     |
| 4 |  3 |  5 | macbook  |    3.23 | book    | macbook   | book    | 4  | 3.23     |
+----+------+------+------------+-------------+------------+-------------+-------------+------+--------------+

This is mainly when grouping multiple fields, you only need to know that the fields behind the grouping order are grouped according to the contents of the previous fields after grouping.

In normal development tasks, we often use GROUP BY grouping of MYSQL to obtain statistical data based on grouping fields in data tables. For example, there is a student course selection table, and the table structure is as follows:

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 reason for this result is:

GROUP BY X means to put all records with the same X field value into one packet.

What about GROUP BY X and Y?

GROUP BY X, Y means to put all records with the same X field value and Y field value into one packet.

Summarize


Related articles: