美文网首页
MYSQL 8 優化之10 (使用group by 的with

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

作者: 轻飘飘D | 来源:发表于2019-08-19 22:13 被阅读0次
  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 |
+------------+----------+

相关文章

网友评论

      本文标题:MYSQL 8 優化之10 (使用group by 的with

      本文链接:https://www.haomeiwen.com/subject/epvmdctx.html