时间:2021-07-01 10:21:17 帮助过:35人阅读
在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。
GROUP BY 语法
SELECT column_name,function(column_name)FROM table_name
WHERE column_name operator value
GROUP BY column_name;
实例演示
本章节实例使用到了以下表结构及数据,使用前我们可以先将以下数据导入数据库中。
MariaDB [RUNOOB]> SET NAMES utf8;Query OK, 0 rows affected (0.00 sec)
MariaDB [RUNOOB]> SET FOREIGN_KEY_CHECKS = 0;Query OK, 0 rows affected (0.00 sec)
MariaDB [RUNOOB]> DROP TABLE IF EXISTS `employee_tbl`;Query OK, 0 rows affected, 1 warning (0.00 sec)MariaDB [RUNOOB]> 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;Query OK, 0 rows affected (0.04 sec)
MariaDB [RUNOOB]> BEGIN;Query OK, 0 rows affected (0.00 sec)
MariaDB [RUNOOB]> INSERT INTO employee_tbl VALUES ('1', 'aa', '2016-04-22 15:25:33', '1'), ('2', 'bb', '2016-04-20 15:25:47', '3'), ('3', 'cc', '2016-04-19 15:26:02', '2'), ('4', 'bb', '2016-04-07 15:26:14', '4'), ('5', 'aa', '2016-04-11 15:26:40', '4'), ('6', 'aa', '2016-04-04 15:26:54', '2');Query OK, 6 rows affected, 6 warnings (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 6
MariaDB [RUNOOB]> COMMIT;Query OK, 0 rows affected (0.00 sec)
MariaDB [RUNOOB]> SET FOREIGN_KEY_CHECKS = 1;Query OK, 0 rows affected (0.00 sec)
导入成功后,执行以下 SQL 语句:
MariaDB [RUNOOB]> select * from employee_tbl;+----+------+---------------------+--------+
| id | name | date | singin |
+----+------+---------------------+--------+
| 1 | aa | 2016-04-22 15:25:33 | 1 |
| 2 | bb | 2016-04-20 15:25:47 | 3 |
| 3 | cc | 2016-04-19 15:26:02 | 2 |
| 4 | bb | 2016-04-07 15:26:14 | 4 |
| 5 | aa | 2016-04-11 15:26:40 | 4 |
| 6 | aa | 2016-04-04 15:26:54 | 2 |
+----+------+---------------------+--------+
6 rows in set (0.00 sec)
接下来我们使用 GROUP BY 语句 将数据表按名字进行分组,并统计每个人有多少条记录:
MariaDB [RUNOOB]> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name;+------+----------+
| name | COUNT(*) |
+------+----------+
| aa | 3 |
| bb | 2 |
| cc | 1 |
+------+----------+
3 rows in set (0.00 sec)
使用 WITH ROLLUP
WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。
例如我们将以上的数据表按名字进行分组,再统计每个人登录的次数:
MariaDB [RUNOOB]> SELECT name, SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;+------+--------------+
| name | singin_count |
+------+--------------+
| aa | 7 |
| bb | 7 |
| cc | 2 |
| NULL | 16 |
+------+--------------+
4 rows in set (0.00 sec)
其中记录 NULL 表示所有人的登录次数。
我们可以使用 coalesce 来设置一个可以取代 NUll 的名称,coalesce 语法:
select coalesce(a,b,c);
参数说明:
如果a!=null,则选择a;
如果a==null,则选择b;
如果b==null,则选择c;
如果a b c 都为null ,则返回为null(没意义)。
以下实例中如果名字为空我们使用总数代替:
MariaDB [RUNOOB]> SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP;+------------------------+--------------+
| coalesce(name, '总数') | singin_count |
+------------------------+--------------+
| aa | 7 |
| bb | 7 |
| cc | 2 |
| 总数 | 16 |
+------------------------+--------------+
4 rows in set (0.00 sec)