{"id":1725,"date":"2025-02-16T08:59:36","date_gmt":"2025-02-16T00:59:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1725"},"modified":"2025-02-16T08:59:36","modified_gmt":"2025-02-16T00:59:36","slug":"sql-sum-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/16\/sql-sum-%e5%87%bd%e6%95%b0\/","title":{"rendered":"SQL SUM() \u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>SUM()<\/code> \u51fd\u6570\u662f\u4e00\u4e2a\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u6307\u5b9a\u5217\u4e2d\u6240\u6709\u6570\u503c\u7684<strong>\u603b\u548c<\/strong>\u3002\u5b83\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u7684\u6570\u503c\u603b\u548c\uff0c\u4f8b\u5982\u603b\u9500\u552e\u989d\u3001\u603b\u5de5\u8d44\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>SUM()<\/code> \u51fd\u6570\u6982\u8ff0<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f5c\u7528<\/strong>\uff1a<code>SUM()<\/code> \u51fd\u6570\u8fd4\u56de\u67d0\u5217\u7684\u603b\u548c\u3002\u5b83\u53ea\u9002\u7528\u4e8e\u6570\u503c\u5217\u3002<\/li>\n\n\n\n<li><strong>\u8bed\u6cd5<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT SUM(column_name)\n  FROM table_name\n  WHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>column_name<\/code>\uff1a\u8981\u8ba1\u7b97\u603b\u548c\u7684\u5217\u3002<\/li>\n\n\n\n<li><code>table_name<\/code>\uff1a\u67e5\u8be2\u7684\u8868\u540d\u3002<\/li>\n\n\n\n<li><code>condition<\/code>\uff1a\u53ef\u9009\u7684\u7b5b\u9009\u6761\u4ef6\uff0c\u7528\u4e8e\u6307\u5b9a\u54ea\u4e9b\u884c\u8981\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>SUM()<\/code> \u51fd\u6570\u7684\u5e94\u7528<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SUM()<\/code> \u51fd\u6570\u5e38\u7528\u4e8e\u8ba1\u7b97\u6570\u503c\u5217\u7684\u603b\u548c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u4f8b\u5b50\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u6570\u503c\u5217\u7684\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>employees<\/code> \u7684\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>employee_id<\/th><th>name<\/th><th>salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Tom<\/td><td>5000<\/td><\/tr><tr><td>2<\/td><td>Jane<\/td><td>6000<\/td><\/tr><tr><td>3<\/td><td>Bob<\/td><td>7000<\/td><\/tr><tr><td>4<\/td><td>Alice<\/td><td>8000<\/td><\/tr><tr><td>5<\/td><td>Mike<\/td><td>5500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u8868\u4e2d\u6240\u6709\u5458\u5de5\u85aa\u6c34\u7684\u603b\u548c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SUM(salary) AS total_salary\nFROM employees;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>total_salary<\/th><\/tr><\/thead><tbody><tr><td>31500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u4e0e <code>GROUP BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SUM()<\/code> \u5e38\u5e38\u4e0e <code>GROUP BY<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u4fbf\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u603b\u548c\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u6bcf\u4e2a\u90e8\u95e8\u7684\u603b\u85aa\u6c34\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department_id, SUM(salary) AS total_salary\nFROM employees\nGROUP BY department_id;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\u6570\u636e<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>department_id<\/th><th>salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>5000<\/td><\/tr><tr><td>1<\/td><td>6000<\/td><\/tr><tr><td>1<\/td><td>7000<\/td><\/tr><tr><td>2<\/td><td>8000<\/td><\/tr><tr><td>2<\/td><td>5500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>department_id<\/th><th>total_salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>18000<\/td><\/tr><tr><td>2<\/td><td>13500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u4e0e <code>HAVING<\/code> \u5b50\u53e5\u4e00\u8d77\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>HAVING<\/code> \u5b50\u53e5\u7528\u4e8e\u7b5b\u9009\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002\u4f60\u53ef\u4ee5\u5728 <code>HAVING<\/code> \u4e2d\u4f7f\u7528 <code>SUM()<\/code> \u51fd\u6570\u6765\u8fc7\u6ee4\u603b\u548c\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u603b\u85aa\u6c34\u8d85\u8fc7 15000 \u7684\u90e8\u95e8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department_id, SUM(salary) AS total_salary\nFROM employees\nGROUP BY department_id\nHAVING SUM(salary) &gt; 15000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u67e5\u8be2\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>department_id<\/th><th>total_salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>18000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong><code>SUM()<\/code> \u8fd4\u56de <code>NULL<\/code><\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u5217\u4e2d\u7684\u6240\u6709\u503c\u90fd\u662f <code>NULL<\/code>\uff0c\u5219 <code>SUM()<\/code> \u4f1a\u8fd4\u56de <code>NULL<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code> \u6216 <code>IFNULL()<\/code> \u6765\u5904\u7406 <code>NULL<\/code> \u503c\u3002\u4f8b\u5982\uff0c\u82e5\u6ca1\u6709\u85aa\u6c34\u8bb0\u5f55\uff0c\u5219\u8fd4\u56de <code>0<\/code>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT COALESCE(SUM(salary), 0) AS total_salary\n  FROM employees;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>\u5728 <code>SUM()<\/code> \u51fd\u6570\u4e2d\u4f7f\u7528 <code>DISTINCT<\/code><\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u95ee\u9898<\/strong>\uff1a\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u8ba1\u7b97\u67d0\u5217\u7684\u603b\u548c\uff0c\u4f46\u53c8\u5e0c\u671b\u53bb\u9664\u91cd\u590d\u7684\u503c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u53ef\u4ee5\u5728 <code>SUM()<\/code> \u51fd\u6570\u4e2d\u4f7f\u7528 <code>DISTINCT<\/code> \u6765\u4ec5\u8ba1\u7b97\u552f\u4e00\u7684\u503c\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT SUM(DISTINCT salary) AS total_salary\n  FROM employees;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong><code>SUM()<\/code> \u51fd\u6570\u548c\u7a7a\u503c\uff08NULL\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u95ee\u9898<\/strong>\uff1a<code>SUM()<\/code> \u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\uff0c\u5b83\u53ea\u4f1a\u8ba1\u7b97\u6709\u6548\u7684\uff08\u975e <code>NULL<\/code>\uff09\u6570\u503c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u786e\u4fdd\u5904\u7406 <code>NULL<\/code> \u503c\u3002\u5982\u679c\u4f60\u5e0c\u671b <code>NULL<\/code> \u88ab\u5f53\u4f5c <code>0<\/code> \u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT SUM(COALESCE(salary, 0)) AS total_salary\n  FROM employees;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>\u8ba1\u7b97\u67d0\u4e00\u7ec4\u4e2d\u603b\u548c\u7684\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u95ee\u9898<\/strong>\uff1a<code>SUM()<\/code> \u51fd\u6570\u5728\u8ba1\u7b97\u805a\u5408\u503c\u65f6\uff0c\u53ef\u80fd\u5728\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u4e0a\u6027\u80fd\u4e0d\u7406\u60f3\uff0c\u5c24\u5176\u662f\u6ca1\u6709\u7d22\u5f15\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u786e\u4fdd\u5bf9\u9700\u8981\u8ba1\u7b97\u603b\u548c\u7684\u5217\u521b\u5efa\u7d22\u5f15\uff0c\u5c24\u5176\u662f\u5f53\u8be5\u5217\u7528\u4e8e <code>WHERE<\/code> \u6216 <code>ORDER BY<\/code> \u5b50\u53e5\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SUM()<\/code> \u53ef\u4ee5\u4e0e\u5176\u4ed6\u805a\u5408\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\uff0c\u5982 <code>AVG()<\/code>, <code>COUNT()<\/code>, <code>MAX()<\/code>, <code>MIN()<\/code> \u7b49\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u67e5\u8be2\u6bcf\u4e2a\u90e8\u95e8\u7684\u603b\u85aa\u6c34\u3001\u5e73\u5747\u85aa\u6c34\u4ee5\u53ca\u5458\u5de5\u603b\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT department_id,\n       SUM(salary) AS total_salary,\n       AVG(salary) AS avg_salary,\n       COUNT(*) AS employee_count\nFROM employees\nGROUP BY department_id;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SUM()<\/code> \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u67d0\u5217\u7684\u603b\u548c\uff0c\u9002\u7528\u4e8e\u6570\u503c\u7c7b\u578b\u7684\u5217\u3002<\/li>\n\n\n\n<li>\u5b83\u5e7f\u6cdb\u5e94\u7528\u4e8e\u8ba1\u7b97\u6570\u636e\u7684\u603b\u548c\uff0c\u5e76\u5e38\u4e0e <code>GROUP BY<\/code> \u548c <code>HAVING<\/code> \u5b50\u53e5\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u5728\u5904\u7406\u7a7a\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>COALESCE()<\/code> \u6216 <code>IFNULL()<\/code> \u6765\u907f\u514d\u8fd4\u56de <code>NULL<\/code>\u3002<\/li>\n\n\n\n<li>\u5728\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u4e0a\uff0c\u786e\u4fdd\u5bf9\u76f8\u5173\u5217\u521b\u5efa\u7d22\u5f15\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SUM() \u51fd\u6570\u662f\u4e00\u4e2a\u805a\u5408\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u6307\u5b9a\u5217\u4e2d\u6240\u6709\u6570\u503c\u7684\u603b\u548c\u3002\u5b83\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u7684\u6570\u503c\u603b\u548c\uff0c\u4f8b\u5982\u603b\u9500\u552e\u989d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1725","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1725"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1725\/revisions"}],"predecessor-version":[{"id":1726,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1725\/revisions\/1726"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}