MYSQL 8 優化之10 (使用group by 的with

2019-08-19  本文已影响0人  轻飘飘D
  1. 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 |
+------------+----------+
上一篇下一篇

猜你喜欢

热点阅读