MySQL GROUP BY代码笔记

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值