{"id":75654,"date":"2024-10-16T00:06:18","date_gmt":"2024-10-15T16:06:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/75654\/"},"modified":"2024-10-16T00:06:19","modified_gmt":"2024-10-15T16:06:19","slug":"mysql-%e4%b8%adwith-rollup%e7%b8%bd%e7%b5%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/75654\/","title":{"rendered":"MySQL \u4e2dWITH ROLLUP\u7e3d\u7d50"},"content":{"rendered":"<h1 id=\"mysql-%e4%b8%ad-with-rollup-%e7%b8%bd%e7%b5%90-IfDHSPluoB\">MySQL \u4e2d WITH ROLLUP \u7e3d\u7d50<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cMySQL \u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u8655\u7406\u548c\u5206\u6790\u6578\u64da\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u4f7f\u7528 <code>WITH ROLLUP<\/code> \u4f86\u751f\u6210\u532f\u7e3d\u5831\u544a\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e <code>WITH ROLLUP<\/code> \u7684\u7528\u6cd5\u53ca\u5176\u5728\u6578\u64da\u5206\u6790\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-with-rollup%ef%bc%9f-IfDHSPluoB\">\u4ec0\u9ebc\u662f WITH ROLLUP\uff1f<\/h2>\n<p><code>WITH ROLLUP<\/code> \u662f\u4e00\u500b SQL \u64f4\u5c55\uff0c\u7528\u65bc\u5728 GROUP BY \u67e5\u8a62\u4e2d\u751f\u6210\u984d\u5916\u7684\u532f\u7e3d\u884c\u3002\u9019\u4e9b\u532f\u7e3d\u884c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c64\u6b21\u7684\u6578\u64da\u805a\u5408\uff0c\u5e6b\u52a9\u7528\u6236\u5feb\u901f\u4e86\u89e3\u6578\u64da\u7684\u6574\u9ad4\u8da8\u52e2\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e8%aa%9e%e6%b3%95-IfDHSPluoB\">\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>\u4f7f\u7528 <code>WITH ROLLUP<\/code> \u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>SELECT column1, column2, SUM(column3)\nFROM table_name\nGROUP BY column1, column2 WITH ROLLUP;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u8a9e\u6cd5\u4e2d\uff0c<code>column1<\/code> \u548c <code>column2<\/code> \u662f\u7528\u65bc\u5206\u7d44\u7684\u5217\uff0c\u800c <code>SUM(column3)<\/code> \u5247\u662f\u9700\u8981\u8a08\u7b97\u7684\u805a\u5408\u51fd\u6578\u3002<\/p>\n<h2 id=\"%e7%a4%ba%e4%be%8b-IfDHSPluoB\">\u793a\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>sales<\/code> \u7684\u8868\u683c\uff0c\u5305\u542b\u4ee5\u4e0b\u6578\u64da\uff1a<\/p>\n<pre><code>+---------+---------+-------+\n| product | region  | sales |\n+---------+---------+-------+\n| A       | East    | 100   |\n| A       | West    | 150   |\n| B       | East    | 200   |\n| B       | West    | 250   |\n+---------+---------+-------+<\/code><\/pre>\n<p>\u5982\u679c\u6211\u5011\u60f3\u8981\u6309\u7522\u54c1\u548c\u5730\u5340\u532f\u7e3d\u92b7\u552e\u984d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT product, region, SUM(sales)\nFROM sales\nGROUP BY product, region WITH ROLLUP;<\/code><\/pre>\n<p>\u57f7\u884c\u6b64\u67e5\u8a62\u5f8c\uff0c\u7d50\u679c\u5c07\u986f\u793a\u6bcf\u500b\u7522\u54c1\u5728\u4e0d\u540c\u5730\u5340\u7684\u92b7\u552e\u984d\uff0c\u4ee5\u53ca\u6bcf\u500b\u7522\u54c1\u7684\u7e3d\u92b7\u552e\u984d\u548c\u6240\u6709\u7522\u54c1\u7684\u7e3d\u92b7\u552e\u984d\uff1a<\/p>\n<pre><code>+---------+---------+-------+\n| product | region  | sales |\n+---------+---------+-------+\n| A       | East    | 100   |\n| A       | West    | 150   |\n| A       | NULL    | 250   |\n| B       | East    | 200   |\n| B       | West    | 250   |\n| B       | NULL    | 450   |\n| NULL    | NULL    | 700   |\n+---------+---------+-------+<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8-with-rollup-%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-IfDHSPluoB\">\u4f7f\u7528 WITH ROLLUP \u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>NULL \u503c\uff1a<\/strong>\u5728\u532f\u7e3d\u884c\u4e2d\uff0c\u5206\u7d44\u5217\u7684\u503c\u5c07\u986f\u793a\u70ba <code>NULL<\/code>\uff0c\u9019\u8868\u793a\u8a72\u884c\u662f\u532f\u7e3d\u7d50\u679c\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u91cf\uff1a<\/strong>\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u4f7f\u7528 <code>WITH ROLLUP<\/code> \u53ef\u80fd\u6703\u5f71\u97ff\u67e5\u8a62\u6027\u80fd\uff0c\u56e0\u6b64\u5728\u8a2d\u8a08\u67e5\u8a62\u6642\u9700\u8b39\u614e\u8003\u91cf\u3002<\/li>\n<li><strong>\u8207\u5176\u4ed6\u805a\u5408\u51fd\u6578\u7d50\u5408\uff1a<\/strong>\u53ef\u4ee5\u8207\u5176\u4ed6\u805a\u5408\u51fd\u6578\uff08\u5982 <code>AVG<\/code>\u3001<code>COUNT<\/code> \u7b49\uff09\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u7372\u5f97\u66f4\u5168\u9762\u7684\u6578\u64da\u5206\u6790\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-IfDHSPluoB\">\u7d50\u8ad6<\/h2>\n<p>MySQL \u4e2d\u7684 <code>WITH ROLLUP<\/code> \u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u5feb\u901f\u751f\u6210\u6578\u64da\u7684\u532f\u7e3d\u5831\u544a\u3002\u901a\u904e\u9069\u7576\u7684\u4f7f\u7528\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u6578\u64da\u7684\u6574\u9ad4\u8da8\u52e2\uff0c\u4e26\u505a\u51fa\u66f4\u660e\u667a\u7684\u6c7a\u7b56\u3002\u7121\u8ad6\u662f\u5728\u5546\u696d\u5206\u6790\u9084\u662f\u6578\u64da\u5831\u544a\u4e2d\uff0c<code>WITH ROLLUP<\/code> \u90fd\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u696d\u52d9\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u4e2d\u7684WITH ROLLUP\u529f\u80fd\uff0c\u5982\u4f55\u5728\u805a\u5408\u67e5\u8a62\u4e2d\u751f\u6210\u5c0f\u8a08\u548c\u7e3d\u8a08\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u7684\u9748\u6d3b\u6027\u8207\u6df1\u5ea6\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-75654","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=75654"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75654\/revisions"}],"predecessor-version":[{"id":75655,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75654\/revisions\/75655"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=75654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=75654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=75654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}