{"id":200357,"date":"2025-05-06T09:51:37","date_gmt":"2025-05-06T01:51:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200357\/"},"modified":"2025-05-06T09:51:37","modified_gmt":"2025-05-06T01:51:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-mysql-%e4%b8%ad%e5%88%86%e7%bb%84%e5%b9%b6%e8%a1%8c%e5%b1%95%e7%a4%ba%e4%b8%8a%e5%8d%8a%e5%b9%b4%e5%92%8c%e4%b8%8b%e5%8d%8a%e5%b9%b4%e6%95%b0%e6%8d%ae%e7%9a%84%e6%b1%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200357\/","title":{"rendered":"\u5982\u4f55\u5728 MySQL \u4e2d\u5206\u7ec4\u5e76\u884c\u5c55\u793a\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u6570\u636e\u7684\u6c47\u603b\u7ed3\u679c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728 MySQL \u4e2d\u5206\u7ec4\u5e76\u884c\u5c55\u793a\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u6570\u636e\u7684\u6c47\u603b\u7ed3\u679c\uff1f<\/h1>\n<p>\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/17305495616726173945bfc.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql \u5982\u4f55\u5206\u7ec4\u5e76\u884c\u5c55\u793a\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u5982\u4f55\u5728 mysql \u4e2d\u6839\u636e\u5206\u7ec4\u6761\u4ef6\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u6307\u5b9a\u987a\u5e8f\u5206\u7ec4\u5e76\u884c\u5c55\u793a\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5047\u8bbe\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>start_date<\/th>\n<th>num<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ceshi<\/td>\n<td>2021-11-22<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>sss<\/td>\n<td>2021-11-21<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>ceshi<\/td>\n<td>2021-01-20<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u76ee\u6807\u7ed3\u679c<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>start_date<\/th>\n<th>shang_sum<\/th>\n<th>xia_sum<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ceshi<\/td>\n<td>\u4e0a\u534a\u5e74<\/td>\n<td>3<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>sss<\/td>\n<td>\u4e0b\u534a\u5e74<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u5b9e\u73b0\u4ee3\u7801<\/strong><\/p>\n<pre>SELECT\n    t.name,\n    start_date,\n    SUM( t.num ) AS shang_sum,\n    IFNULL(t2.xia_sum,0)\nFROM\n    `task` t\n    LEFT JOIN ( SELECT NAME, SUM( num ) xia_sum FROM task WHERE MONTH ( start_date ) &amp;lt; 7 GROUP BY `name` ) t2 ON t2.NAME = t.NAME \nWHERE\n    MONTH ( start_date ) &amp;gt;= 7 \nGROUP BY\n    `name`<\/pre>\n<p><strong>\u4ee3\u7801\u89e3\u6790<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 left join \u5c06\u4e0a\u534a\u5e74\u7684\u6570\u636e\u4e0e\u4e0b\u534a\u5e74\u7684\u6570\u636e\u8fdb\u884c\u5173\u8054\u3002<\/li>\n<li>sum( t.num ) \u8ba1\u7b97\u6307\u5b9a\u65e5\u671f\u6bb5\u5185\u6307\u5b9a\u5206\u7ec4\u6761\u4ef6\u4e0b num \u5b57\u6bb5\u7684\u548c\u3002<\/li>\n<li>ifnull(t2.xia_sum,0) \u5bf9\u4e8e\u4e0a\u534a\u5e74\u5206\u7ec4\uff0c\u8fd4\u56de xia_sum \u7684\u503c\uff0c\u5bf9\u4e8e\u4e0b\u534a\u5e74\u5206\u7ec4\uff0c\u8fd4\u56de 0\u3002<\/li>\n<li>group by \u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u7167 name \u5b57\u6bb5\u5206\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u5728 MySQL \u4e2d\u5206\u7ec4\u5e76\u884c\u5c55\u793a\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u6570\u636e\u7684\u6c47\u603b\u7ed3\u679c\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6570\u636e\u5e93\u77e5\u8bc6\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 MySQL \u4e2d\u5206\u7ec4\u5e76\u884c\u5c55&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200357","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200357","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200357"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200357\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}