{"id":6651,"date":"2024-11-14T09:29:54","date_gmt":"2024-11-14T01:29:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6651\/"},"modified":"2024-11-14T09:29:54","modified_gmt":"2024-11-14T01:29:54","slug":"group-by-rollup%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6651\/","title":{"rendered":"group by rollup\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  group by rollup \u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u6309\u5217\u5206\u7ec4\u5e76\u6c47\u603b\uff0c\u521b\u5efa\u4e0d\u540c\u5c42\u6b21\u7684\u6c47\u603b\uff0c\u4ece\u6700\u8be6\u7ec6\u5230\u6700\u6c47\u603b\uff0c\u5305\u62ec\u6309\u5404\u5217\u5206\u7ec4\u4ee5\u53ca\u6240\u6709\u884c\u7684\u6c47\u603b\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u548c\u6570\u636e\u63a2\u7d22\u7684\u7075\u6d3b\u6027\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202409\/11\/2024091107283987674.jpg\" class=\"aligncenter\" title=\"group by rollup\u51fd\u6570\u63d2\u56fe\" alt=\"group by rollup\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>GROUP BY ROLLUP\uff1a\u4e86\u89e3\u5176\u7528\u9014\u548c\u793a\u4f8b<\/strong><\/p>\n<p><strong>GROUP BY ROLLUP \u51fd\u6570<\/strong><\/p>\n<p>GROUP BY ROLLUP \u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u6309\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\u5e76\u6c47\u603b\uff0c\u5e76\u521b\u5efa\u6c47\u603b\u7684\u4e0d\u540c\u5c42\u6b21\u7ed3\u6784\u3002\u5b83\u5141\u8bb8\u60a8\u67e5\u770b\u6570\u636e\u7684\u5206\u7ec4\u548c\u6c47\u603b\uff0c\u4ece\u6700\u8be6\u7ec6\u7684\u7ea7\u522b\uff08\u4ec5\u5305\u542b\u7279\u5b9a\u5217\u7684\u552f\u4e00\u503c\uff09\u5230\u6700\u6c47\u603b\u7684\u7ea7\u522b\uff08\u5305\u542b\u6240\u6709\u884c\uff09\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>GROUP BY ROLLUP(column_list)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>column_list \u662f\u8981\u5206\u7ec4\u7684\u5217\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u6570\u636e\u7684\u8868\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u8ba2\u5355\u53f7<\/th>\n<th>\u4ea7\u54c1<\/th>\n<th>\u4ef7\u683c<\/th>\n<th>\u9500\u552e\u65e5\u671f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u624b\u673a<\/td>\n<td>100<\/td>\n<td>2023-03-01<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u624b\u673a<\/td>\n<td>200<\/td>\n<td>2023-03-05<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u7535\u8111<\/td>\n<td>500<\/td>\n<td>2023-03-10<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u7535\u8111<\/td>\n<td>600<\/td>\n<td>2023-03-12<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 GROUP BY ROLLUP \u51fd\u6570\u6309\u4ea7\u54c1\u5206\u7ec4\u5e76\u6c47\u603b\u4ef7\u683c\uff1a<\/p>\n<pre>SELECT product, SUM(price) AS total_price\nFROM orders\nGROUP BY ROLLUP(product);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1<\/th>\n<th>\u603b\u4ef7<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u624b\u673a<\/td>\n<td>300<\/td>\n<\/tr>\n<tr>\n<td>\u7535\u8111<\/td>\n<td>1100<\/td>\n<\/tr>\n<tr>\n<td>NULL<\/td>\n<td>1400<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6b64\u7ed3\u679c\u663e\u793a\u4e86\u4ea7\u54c1\u7684\u603b\u4ef7\u683c\uff0c\u4ee5\u53ca\u6309\u4ea7\u54c1\u5206\u7ec4\u548c\u6240\u6709\u884c\u7684\u6c47\u603b\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>GROUP BY ROLLUP \u51fd\u6570\u63d0\u4f9b\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u521b\u5efa\u6c47\u603b\u5c42\u6b21\u7ed3\u6784\uff1a<\/strong> \u5b83\u5141\u8bb8\u60a8\u521b\u5efa\u4e0d\u540c\u5c42\u7ea7\u7684\u6c47\u603b\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u770b\u6570\u636e\u7684\u4e0d\u540c\u6c47\u603b\u89c6\u56fe\u3002<\/li>\n<li> <strong>\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\uff1a<\/strong> \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528 GROUP BY ROLLUP \u53ef\u4ee5\u6bd4\u4f7f\u7528\u591a\u4e2a GROUP BY \u67e5\u8be2\u66f4\u9ad8\u6548\u3002<\/li>\n<li> <strong>\u589e\u5f3a\u6570\u636e\u63a2\u7d22\uff1a<\/strong> \u5b83\u6709\u52a9\u4e8e\u6570\u636e\u63a2\u7d22\uff0c\u56e0\u4e3a\u5b83\u4f7f\u60a8\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u6570\u636e\u7684\u4e0d\u540c\u6c47\u603b\u8868\u793a\u3002<\/li>\n<\/ul>\n<p><strong>\u5c40\u9650\u6027<\/strong><\/p>\n<p>GROUP BY ROLLUP \u51fd\u6570\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a<\/p>\n<ul>\n<li> <strong>\u6027\u80fd\u95ee\u9898\uff1a<\/strong> \u5728\u6570\u636e\u96c6\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li> <strong>\u6570\u636e\u91cd\u590d\uff1a<\/strong> \u5f53\u4f7f\u7528\u591a\u4e2a\u5206\u7ec4\u5217\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u91cd\u590d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fgroup by rollup\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>group by rollup \u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u6309\u5217\u5206\u7ec4\u5e76\u6c47\u603b\uff0c\u521b\u5efa\u4e0d\u540c\u5c42\u6b21\u7684\u6c47\u603b\uff0c\u4ece\u6700\u8be6\u7ec6\u5230\u6700\u6c47\u603b\uff0c\u5305\u62ec\u6309\u5404\u5217\u5206\u7ec4\u4ee5\u53ca\u6240\u6709\u884c\u7684\u6c47\u603b\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u548c\u6570\u636e\u63a2\u7d22\u7684\u7075\u6d3b\u6027\u3002 GROUP BY ROLLUP\uff1a\u4e86\u89e3\u5176\u7528\u9014\u548c\u793a\u4f8b GROUP BY ROLLUP \u51fd\u6570 GROUP BY ROLLUP \u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u6309\u6307\u5b9a\u5217\u8fdb\u884c\u5206\u7ec4\u5e76\u6c47\u603b\uff0c\u5e76\u521b\u5efa\u6c47\u603b\u7684\u4e0d\u540c\u5c42\u6b21\u7ed3\u6784\u3002\u5b83\u5141\u8bb8\u60a8\u67e5\u770b\u6570\u636e\u7684\u5206\u7ec4\u548c\u6c47\u603b\uff0c\u4ece\u6700\u8be6\u7ec6\u7684\u7ea7\u522b\uff08\u4ec5\u5305\u542b\u7279\u5b9a\u5217\u7684\u552f\u4e00\u503c\uff09\u5230\u6700\u6c47\u603b\u7684\u7ea7\u522b\uff08\u5305\u542b\u6240\u6709\u884c\uff09\u3002 \u8bed\u6cd5 GROUP BY ROLLUP(column_list) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_list \u662f\u8981\u5206\u7ec4\u7684\u5217\u5217\u8868\u3002 \u793a\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u6570\u636e\u7684\u8868\uff1a \u8ba2\u5355\u53f7 \u4ea7\u54c1 \u4ef7\u683c \u9500\u552e\u65e5\u671f 1 \u624b\u673a 100 2023-03-01 2 \u624b\u673a 200 2023-03-05 3 \u7535\u8111 500 2023-03-10 4 \u7535\u8111 600 2023-03-12 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 GROUP BY ROLLUP \u51fd\u6570\u6309\u4ea7\u54c1\u5206\u7ec4\u5e76\u6c47\u603b\u4ef7\u683c\uff1a SELECT product, SUM(price) AS total_price [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6651","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6651"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6651\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}