Mysql group by常用操作

1、group by + group_concat()的栗子

group_concat()可以將分組後每個組內的值都顯示出來

group_concat()此函數返回一個字符串,是查詢結果集合中指定列非NULL值的串聯。如果所有列都是NULL,則此函數返回NULL。完整語法如下:

GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

GROUP_CONCAT 是一個聚合函數,通常跟 GROUP BY 一起使用。

具體用法參見:

1、https://www.jianshu.com/p/df54726d63e9

2、https://www.yangdx.com/2019/02/11.html

2、group by +聚合函數的栗子

有什麼聚合函數?

  • count():統計記錄的條數
  • sum():字段值的總和
  • max():字段值的最大值
  • min():字段值的最小值
  • avg():字段值的平均值

3、group by + with rollup的栗子

with rollup用來在所有記錄的最後加上一條記錄,顯示上面所有記錄每個字段的總和,通過一個例子來說明把。

表中數據有:

mysql> select * from age;
+------+-----------+------+
| sno  | sname     | sage |
+------+-----------+------+
| 1101 | justcode1 |   20 |
| 1102 | justcode2 |   21 |
| 1103 | justcode3 |   22 |
| 1104 | justcode4 |   20 |
| 1105 | justcode5 |   21 |
| 1106 | justcode6 |   21 |
| 1107 | justcode7 |   22 |
| 1108 | justcode8 |   22 |
+------+-----------+------+
8 rows in set (0.00 sec)
--------------------- 

沒有with rollup的查詢:

mysql> select count(*),sage from age group by sage;
+----------+------+
| count(*) | sage |
+----------+------+
|        2 |   20 |
|        3 |   21 |
|        3 |   22 |
+----------+------+
3 rows in set (0.00 sec)
--------------------- 

帶with rollup的查詢:

mysql> select count(*),sage from age group by sage with rollup;
+----------+------+
| count(*) | sage |
+----------+------+
|        2 |   20 |
|        3 |   21 |
|        3 |   22 |
|        8 | NULL |
+----------+------+
4 rows in set (0.00 sec)
--------------------- 

with rollup 、with cube、grouping

CUBE 和 ROLLUP 之間的區別在於:

CUBE 生成的結果集顯示了所選列中值的所有組合的聚合。

ROLLUP 生成的結果集顯示了所選列中值的某一層次結構的聚合。

grouping: 當用 CUBE 或 ROLLUP 運算符添加行時,附加的列輸出值爲1,當所添加的行不是由 CUBE 或 ROLLUP 產生時,附加列值爲0。

查看:

1、https://justcode.ikeepstudying.com/2019/03/mysql-%E5%88%86%E7%BB%84%E8%AE%A1%E6%B1%87-mysql%E4%B8%ADwith-rollup%E7%9A%84%E7%94%A8%E6%B3%95-group-by%E6%B1%87%E6%80%BB/

3、mysql 統計 group by 之後的 group 的個數

如果將 count(*) 和 group by 一起使用,count(*) 統計的將會是每個 group 裏面的行數,而不是 group 的個數。

如果你想統計 group 的個數,需要將 group by 查詢放到子查詢裏面,然後在主查詢裏面再使用 count(*)。

如下所示,第一個查詢的 count(*) 統計的只是特定的 Category 和 Year 下的總行數,而第二個查詢纔是第一個查詢的 group 的個數。

mysql> SELECT tag AS Category, YEAR(created) AS Year,
   COUNT(*) AS Counts FROM fyi_links GROUP BY tag,
   YEAR(created);
+----------+------+--------+
| Category | Year | Counts |
+----------+------+--------+
| DBA      | 2005 |      1 |
| DBA      | 2006 |      2 |
| DEV      | 2004 |      1 |
| DEV      | 2006 |      1 |
| SQA      | 2003 |      1 |
| SQA      | 2006 |      1 |
+----------+------+--------+
6 rows in set (0.00 sec)
 
mysql> SELECT COUNT(*) FROM (
   SELECT tag AS Category, YEAR(created) AS Year,
   COUNT(*) AS Counts FROM fyi_links GROUP BY tag,
   YEAR(created) ) groups;
+----------+
| COUNT(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

 

Group by 相關用法參看:

1、https://www.cnblogs.com/duhuo/p/5110590.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章