簡略引見MySQL中GROUP BY子句的應用。本站提示廣大學習愛好者:(簡略引見MySQL中GROUP BY子句的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略引見MySQL中GROUP BY子句的應用正文
可使用GROUP BY組值一列,而且假如情願的話,可以將該列停止盤算。應用COUNT,SUM,AVG等功效的分組列。
要懂得GROUP BY子句斟酌的EMPLOYEE_TBL的的表具有以下記載:
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
如今,假定依據上表,我們願望盤算每位員工任務的天數。
假如我們將編寫一個SQL查詢,以下所示,那末我們將獲得上面的成果:
mysql> SELECT COUNT(*) FROM employee_tbl; +---------------------------+ | COUNT(*) | +---------------------------+ | 7 | +---------------------------+
然則,這不是我們的目標辦事,我們要顯示輸出的每一個人零丁的頁面總數。這是經由過程應用聚合函數一路用GROUP BY子句以下:
mysql> SELECT name, COUNT(*) -> FROM employee_tbl -> GROUP BY name; +------+----------+ | name | COUNT(*) | +------+----------+ | Jack | 2 | | Jill | 1 | | John | 1 | | Ram | 1 | | Zara | 2 | +------+----------+ 5 rows in set (0.04 sec)