真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

MySQLgroupby對單字分組序和多字段分組的方法講解

我這里創(chuàng)建了一個 goods 表,先看下里面的數(shù)據(jù):

在桐城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、做網(wǎng)站 網(wǎng)站設(shè)計制作按需定制,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,桐城網(wǎng)站建設(shè)費(fèi)用合理。

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、根據(jù)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 |
+----+------+------+------------+-------------+------------+------------------+---------------+-------+----------------+

這里使用了 group_concat()函數(shù),主要目的是為了顯示分組的詳細(xì)信息

上面的根據(jù)單個字段分組很簡單,把相同s_id的記錄都?xì)w組了

2、根據(jù)s_id,goods_desc字段分組

分析:這里查詢分組時,會先根據(jù)s_id分組,然后對每個組里面的數(shù)據(jù)再根據(jù)goods_desc進(jìn)行分組

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  |
+----+------+------+------------+-------------+------------+------------------+-------------+------+--------------+

這里的goods_descs 和 上面的一比較就明白了

接下來還可以再根據(jù) 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     |
+----+------+------+------------+-------------+------------+-------------+-------------+------+--------------+

這里主要是進(jìn)行多個字段分組的時候,只需掌握分組順序后面的字段是根據(jù)前面字段分組后的內(nèi)容再進(jìn)行的分組即可。

在平時的開發(fā)任務(wù)中我們經(jīng)常會用到MYSQL的GROUP BY分組, 用來獲取數(shù)據(jù)表中以分組字段為依據(jù)的統(tǒng)計數(shù)據(jù)。比如有一個學(xué)生選課表,表結(jié)構(gòu)如下:

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

我們想統(tǒng)計每門課程有多少個學(xué)生報名,應(yīng)用如下SQL:

SELECT Subject, Count(*)
FROM Subject_Selection
GROUP BY Subject

得到如下結(jié)果:

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

因?yàn)楸砝镉涗浟擞?個學(xué)生選擇ITB001,2個學(xué)生選擇了MKB114。

產(chǎn)生這個結(jié)果的原因是:

GROUP BY X意思是將所有具有相同X字段值的記錄放到一個分組里。

那么GROUP BY X, Y呢?

GROUP BY X, Y意思是將所有具有相同X字段值和Y字段值的記錄放到一個分組里。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接


網(wǎng)站標(biāo)題:MySQLgroupby對單字分組序和多字段分組的方法講解
標(biāo)題路徑:http://weahome.cn/article/jgghjs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部