MYSQL 8 優化之10 (使用group by 的with
2019-08-19 本文已影响0人
轻飘飘D
- group by 的with rollup
root@127.0.0.1 : testdb【11:22:28】12 SQL->select country_id,count(1) from city group by country_id;
+------------+----------+
| country_id | count(1) |
+------------+----------+
| 1001 | 4 |
| 1002 | 4 |
| 1003 | 2 |
| 1004 | 2 |
| 1005 | 3 |
+------------+----------+
#先對 country_id 分組 再 union all 全部統計數據
root@127.0.0.1 : testdb【11:22:49】13 SQL->select country_id,count(1) from city group by country_id with rollup;
+------------+----------+
| country_id | count(1) |
+------------+----------+
| 1001 | 4 |
| 1002 | 4 |
| 1003 | 2 |
| 1004 | 2 |
| 1005 | 3 |
| NULL | 15 |
+------------+----------+