English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The GROUP BY statement groups the result set based on one or more columns.
On the columns of the grouping, we can use COUNT, SUM, AVG, and other functions.
SELECT column_name, function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
This chapter example uses the following table structure and data, we can import the following data into the database before using it.
SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for `employee_tbl` -- ---------------------------- DROP TABLE IF EXISTS `employee_tbl`; CREATE TABLE `employee_tbl` ( `id` int(11) NOT NULL, `name` char(10) NOT NULL DEFAULT '', `date` datetime NOT NULL, `singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of `employee_tbl` -- ---------------------------- BEGIN; INSERT INTO `employee_tbl` VALUES ('1', 'Xiao Ming', ''2016-04-22 15:25:33', ''1), ('2', 'Xiao Wang', ''2016-04-20 15:25:47', ''3), ('3', 'Xiao Li', ''2016-04-19 15:26:02', ''2), ('4', 'Xiao Wang', ''2016-04-07 15:26:14', ''4), ('5', 'Xiao Ming', ''2016-04-11 15:26:40', ''4), ('6', 'Xiao Ming', ''2016-04-04 15:26:54', ''2); COMMIT; SET FOREIGN_KEY_CHECKS = 1;
Nachdem das Importieren erfolgreich war, führen Sie die folgenden SQL-Anweisungen aus:
mysql> set names utf8; mysql> SELECT * FROM employee_tbl; +----+--------+---------------------+--------+ | id | name | date | Datum | singin | +----+--------+---------------------+--------+ | 1 | Xiao Ming | 2016-04-22 15:25:33 | 1 | | 2 | Xiao Wang | 2016-04-20 15:25:47 | 3 | | 3 | Xiao Li | 2016-04-19 15:26:02 | 2 | | 4 | Xiao Wang | 2016-04-07 15:26:14 | 4 | | 5 | Xiao Ming | 2016-04-11 15:26:40 | 4 | | 6 | Xiao Ming | 2016-04-04 15:26:54 | 2 | +----+--------+---------------------+--------+ 6 Zeilen in der Sammlung (0,00 sec)
Als nächstes verwenden wir die GROUP BY-Anweisung, um die Tabelle nach Namen zu gruppieren und die Anzahl der Zeilen jeder Person zu zählen:
mysql> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name; +--------+----------+ | name | COUNT(*) | +--------+----------+ | Xiao Li | 1 | | Xiao Ming | 3 | | Xiao Wang | 2 | +--------+----------+ 3 Zeilen in Set (0.01 sec)
WITH ROLLUP ermöglicht es, auf den gruppierten Statistikdaten eine gleiche Statistik durchzuführen (SUM, AVG, COUNT ...).
Zum Beispiel gruppiert wir die obige Tabelle nach Namen und zählen wir die Anzahl der Anmeldevorgänge jeder Person:
mysql> SELECT name, SUM(singin) AS singin_count FROM employee_tbl GROUP BY name WITH ROLLUP; +--------+--------------+ | name | singin_count | +--------+--------------+ | Xiao Li | 2 | | Xiao Ming | 7 | | Xiao Wang | 7 | | NULL | 16 | +--------+--------------+ 4 Zeilen in der Sammlung (0,00 sec)
Dabei stellt NULL die Anmeldezeiten aller Personen dar.
Wir können coalesce verwenden, um einen Namen zu setzen, der für NULL ersetzt werden kann, coalesce Syntax:
select coalesce(a,b,c);
Parameterbeschreibung: Wenn a==null, wird b gewählt; wenn b==null, wird c gewählt; wenn a!=null, wird a gewählt; wenn a, b und c alle null sind, wird null zurückgegeben (kein Sinn).
Im folgenden Beispiel wird, wenn der Name leer ist, 'Gesamtzahl' verwendet:
mysql> SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP; +--------------------------+--------------+ | coalesce(name, '总数') | singin_count | +--------------------------+--------------+ | 小丽 | 2 | | 小明 | 7 | | 小王 | 7 | | 总数 | 16 | +--------------------------+--------------+ 4 Zeilen in Set (0.01 sec)