{"id":712,"date":"2025-07-24T11:25:29","date_gmt":"2025-07-24T03:25:29","guid":{"rendered":"https:\/\/www.hgfwq.com\/blog\/?p=712"},"modified":"2025-07-24T11:25:29","modified_gmt":"2025-07-24T03:25:29","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8mysql%e4%b8%ad%e6%ad%a3%e7%a1%ae%e4%bd%bf%e7%94%a8sum%e5%87%bd%e6%95%b0%e8%bf%9b%e8%a1%8c%e5%a4%8d%e6%95%b0%e6%b1%82%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.hgfwq.com\/blog\/712.html","title":{"rendered":"\u5982\u4f55\u5728MySQL\u4e2d\u6b63\u786e\u4f7f\u7528SUM\u51fd\u6570\u8fdb\u884c\u590d\u6570\u6c42\u548c"},"content":{"rendered":"<p><h>MySQL SUM \u51fd\u6570\u8be6\u89e3<\/h><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.hgfwq.com\/blog\/wp-content\/uploads\/2025\/07\/NRfVQ8019.jpg\" alt=\"\u5982\u4f55\u5728MySQL\u4e2d\u6b63\u786e\u4f7f\u7528SUM\u51fd\u6570\u8fdb\u884c\u590d\u6570\u6c42\u548c\" title=\"\u5982\u4f55\u5728MySQL\u4e2d\u6b63\u786e\u4f7f\u7528SUM\u51fd\u6570\u8fdb\u884c\u590d\u6570\u6c42\u548c\"><\/p>\n<p><p>MySQL \u662f\u4e00\u6b3e\u6d41\u884c\u7684\u5f00\u6e90\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u670d\u52a1\u5668\u548c\u4e3b\u673a\u73af\u5883\u4e2d\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230 SUM \u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u5217\u6570\u636e\u7684\u603b\u548c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd MySQL \u4e2d\u7684 SUM \u51fd\u6570\u53ca\u5176\u5e94\u7528\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h>1. SUM \u51fd\u6570\u7b80\u4ecb<\/h><\/p>\n<p><p>SUM \u51fd\u6570\u662f MySQL \u4e2d\u7684\u4e00\u79cd\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u5217\u6216\u591a\u5217\u6570\u636e\u7684\u603b\u548c\u3002\u5728 SELECT \u8bed\u53e5\u4e2d\uff0cSUM \u51fd\u6570\u53ef\u4ee5\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\uff08\u5982 COUNT\u3001MAX\u3001MIN \u7b49\uff09\u4e00\u8d77\u4f7f\u7528\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7edf\u8ba1\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h>2. SUM \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5<\/h><\/p>\n<p><p>\u4ee5\u4e0b\u662f SUM \u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p>&#8220;`sql<\/p>\n<p>SELECT SUM(column_name) FROM table_name;<\/p>\n<p>&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`column_name` \u8868\u793a\u8981\u8ba1\u7b97\u603b\u548c\u7684\u5217\u540d\uff0c`table_name` \u8868\u793a\u6570\u636e\u6240\u5728\u7684\u8868\u540d\u3002<\/p>\n<p><h>3. SUM \u51fd\u6570\u7684\u5e94\u7528\u5b9e\u4f8b<\/h><\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b SUM \u51fd\u6570\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5176\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>3.1 \u8ba1\u7b97\u67d0\u4e00\u5217\u7684\u603b\u548c<\/p>\n<\/p>\n<p>&#8220;`sql<\/p>\n<p>SELECT SUM(price) AS total_price FROM products;<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u8ba1\u7b97 products \u8868\u4e2d price \u5217\u7684\u603b\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u547d\u540d\u4e3a total_price\u3002<\/p>\n<p><p>3.2 \u8ba1\u7b97\u591a\u4e2a\u5217\u7684\u603b\u548c<\/p>\n<\/p>\n<p>&#8220;`sql<\/p>\n<p>SELECT SUM(price) + SUM(discount) AS total_sum FROM products;<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u8ba1\u7b97 products \u8868\u4e2d price \u548c discount \u5217\u7684\u603b\u548c\uff0c\u5e76\u5c06\u7ed3\u679c\u547d\u540d\u4e3a total_sum\u3002<\/p>\n<p><p>3.3 \u5bf9\u5206\u7ec4\u6570\u636e\u8fdb\u884c\u6c42\u548c<\/p>\n<\/p>\n<p>&#8220;`sql<\/p>\n<p>SELECT category, SUM(price) AS total_price FROM products GROUP BY category;<\/p>\n<p>&#8220;`<\/p>\n<p>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u6309 category \u5217\u5bf9 products \u8868\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7c7b\u7684\u603b\u4ef7\uff0c\u5c06\u7ed3\u679c\u547d\u540d\u4e3a total_price\u3002<\/p>\n<p><h>4. SUM \u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>4.1 NULL \u503c\u7684\u5904\u7406<\/p>\n<\/p>\n<p>\u5728\u8ba1\u7b97\u603b\u548c\u65f6\uff0c\u5982\u679c\u5217\u4e2d\u5b58\u5728 NULL \u503c\uff0cMySQL \u4f1a\u5c06\u5176\u89c6\u4e3a 0\u3002\u8fd9\u610f\u5473\u7740 SUM \u51fd\u6570\u4f1a\u81ea\u52a8\u5ffd\u7565 NULL \u503c\uff0c\u53ea\u8ba1\u7b97\u975e NULL \u503c\u7684\u603b\u548c\u3002<\/p>\n<p><p>4.2 \u805a\u5408\u51fd\u6570\u7684\u4f18\u5148\u7ea7<\/p>\n<\/p>\n<p>\u5728 SELECT \u8bed\u53e5\u4e2d\uff0c\u805a\u5408\u51fd\u6570\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u5176\u4ed6\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u5148\u8ba1\u7b97 SUM \u51fd\u6570\u7684\u7ed3\u679c\uff0c\u7136\u540e\u518d\u5c06\u5176\u4e0e COUNT \u51fd\u6570\u7684\u7ed3\u679c\u76f8\u4e58\uff1a<\/p>\n<\/p>\n<p>&#8220;`sql<\/p>\n<p>SELECT SUM(price)  COUNT() AS total FROM products;<\/p>\n<p>&#8220;`<\/p>\n<p><h>5. \u603b\u7ed3<\/h><\/p>\n<p><p>MySQL \u7684 SUM \u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u805a\u5408\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u8ba1\u7b97\u4e00\u5217\u6216\u591a\u5217\u6570\u636e\u7684\u603b\u548c\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u638c\u63e1\u4e86 SUM \u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u8fd0\u7528 SUM \u51fd\u6570\uff0c\u5b9e\u73b0\u66f4\u4e30\u5bcc\u7684\u6570\u636e\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h>\u95ee\u7b54\u73af\u8282<\/h><\/p>\n<p><b>\u95ee\uff1aSUM \u51fd\u6570\u5728\u8ba1\u7b97\u603b\u548c\u65f6\uff0c\u5982\u4f55\u5904\u7406 NULL \u503c\uff1f<\/b><\/p>\n<p>\u7b54\uff1a\u5728\u8ba1\u7b97\u603b\u548c\u65f6\uff0cSUM \u51fd\u6570\u4f1a\u5c06 NULL \u503c\u89c6\u4e3a 0\uff0c\u53ea\u8ba1\u7b97\u975e NULL \u503c\u7684\u603b\u548c\u3002<\/p>\n<p><b>\u95ee\uff1a\u5982\u4f55\u5bf9\u5206\u7ec4\u6570\u636e\u8fdb\u884c\u6c42\u548c\uff1f<\/b><\/p>\n<p>\u7b54\uff1a\u53ef\u4ee5\u4f7f\u7528 GROUP BY \u5b50\u53e5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 SUM \u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u6570\u636e\u603b\u548c\u3002<\/p>\n<p><b>\u95ee\uff1a\u5728 SELECT \u8bed\u53e5\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528 SUM \u51fd\u6570\u8ba1\u7b97\u591a\u4e2a\u5217\u7684\u603b\u548c\uff1f<\/b><\/p>\n<p>\u7b54\uff1a\u53ef\u4ee5\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528\u591a\u4e2a SUM \u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u5b9e\u73b0\u8ba1\u7b97\u591a\u4e2a\u5217\u7684\u603b\u548c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL SUM \u51fd\u6570\u8be6\u89e3 MySQL \u662f\u4e00\u6b3e\u6d41\u884c\u7684\u5f00\u6e90\u5173\u7cfb\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u670d\u52a1\u5668\u548c\u4e3b\u673a\u73af\u5883\u4e2d\u3002\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230 SUM \u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u5217\u6570\u636e\u7684\u603b\u548c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd MySQL \u4e2d\u7684 SUM \u51fd\u6570\u53ca\u5176\u5e94\u7528\uff0c\u5e2e\u52a9\u60a8\u66f4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":713,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[326,325,70],"topic":[],"class_list":["post-712","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haiwai","tag-mysql","tag-sum","tag-70"],"_links":{"self":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":1,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/712\/revisions\/715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media\/713"}],"wp:attachment":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/tags?post=712"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/topic?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}