mysql> insert into testB values
-> (9,'JAVA','某年','1018-02-28'),
-> (6,'PHP','某月','1068-02-28'),
-> (8,'C#','某日','1168-02-28');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from testB;
+----+-------+---------+------------+
| id | name | address | time |
+----+-------+---------+------------+
| 1 | c语言 | 昨天 | 2011-09-09 |
| 2 | MySQL | 今天 | 2018-06-06 |
| 3 | C++ | 明天 | 2018-09-09 |
| 4 | GO | 未来 | 2019-09-08 |
| 6 | PHP | 某月 | 1068-02-28 |
| 8 | C# | 某日 | 1168-02-28 |
| 9 | JAVA | 某年 | 1018-02-28 |
+----+-------+---------+------------+
7 rows in set (0.01 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into testB values
-> (12,'BAISC','某刻','1268-02-28');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from testB;
+----+-------+---------+------------+
| id | name | address | time |
+----+-------+---------+------------+
| 1 | c语言 | 昨天 | 2011-09-09 |
| 2 | MySQL | 今天 | 2018-06-06 |
| 3 | C++ | 明天 | 2018-09-09 |
| 4 | GO | 未来 | 2019-09-08 |
| 6 | PHP | 某月 | 1068-02-28 |
| 8 | C# | 某日 | 1168-02-28 |
| 9 | JAVA | 某年 | 1018-02-28 |
| 12 | BAISC | 某刻 | 1268-02-28 |
+----+-------+---------+------------+
8 rows in set (0.00 sec)
mysql> select name as '名称',count(*) as '数量' from testB group by name;
+-------+------+
| 名称 | 数量 |
+-------+------+
| BAISC | 1 |
| C# | 1 |
| C++ | 1 |
| c语言 | 1 |
| GO | 1 |
| JAVA | 1 |
| MySQL | 1 |
| PHP | 1 |
+-------+------+
8 rows in set (0.03 sec)
mysql> insert into testB values
-> (5,'C++','某刻','1668-02-28');
Query OK, 1 row affected (0.00 sec)
mysql> select name as '名称',count(*) as '数量' from testB group by name;
+-------+------+
| 名称 | 数量 |
+-------+------+
| BAISC | 1 |
| C# | 1 |
| C++ | 2 |
| c语言 | 1 |
| GO | 1 |
| JAVA | 1 |
| MySQL | 1 |
| PHP | 1 |
+-------+------+
8 rows in set (0.00 sec)
//使用 WITH ROLLUP
//WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。
mysql> select name as '名称',count(*) as '数量' from testB group by name with rollup;
+-------+------+
| 名称 | 数量 |
+-------+------+
| BAISC | 1 |
| C# | 1 |
| C++ | 2 |
| c语言 | 1 |
| GO | 1 |
| JAVA | 1 |
| MySQL | 1 |
| PHP | 1 |
| NULL | 9 |
+-------+------+
9 rows in set (0.01 sec)
//可以使用 coalesce 来设置一个可以取代 NUll 的名称,coalesce 语法
//select coalesce(a,b,c);
//参数说明:
//如果a==null,则选择b;如果b==null,则选择c;
//如果a!=null,则选择a;如果a b c 都为null ,则返回为null(没意义)。
mysql> select coalesce('名称','总数'),count(*) as '数量' from testB group by name with rollup;
+-------------------------+------+
| coalesce('名称','总数') | 数量 |
+-------------------------+------+
| 名称 | 1 |
| 名称 | 1 |
| 名称 | 2 |
| 名称 | 1 |
| 名称 | 1 |
| 名称 | 1 |
| 名称 | 1 |
| 名称 | 1 |
| 名称 | 9 |
+-------------------------+------+
9 rows in set (0.01 sec)
mysql> select coalesce(name,'总数'),count(*) as '数量' from testB group by name with rollup;
+-----------------------+------+
| coalesce(name,'总数') | 数量 |
+-----------------------+------+
| BAISC | 1 |
| C# | 1 |
| C++ | 2 |
| c语言 | 1 |
| GO | 1 |
| JAVA | 1 |
| MySQL | 1 |
| PHP | 1 |
| 总数 | 9 |
+-----------------------+------+
9 rows in set (0.00 sec)