{"id":6911,"date":"2024-11-14T11:10:14","date_gmt":"2024-11-14T03:10:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6911\/"},"modified":"2024-11-14T11:10:14","modified_gmt":"2024-11-14T03:10:14","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:\/\/fwq.ai\/blog\/6911\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173007631847315.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/p>\n<p><strong> \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 ) &lt; 7 GROUP BY `name` ) t2 ON t2.NAME = t.NAME \nWHERE\n    MONTH ( start_date ) &gt;= 7 \nGROUP BY\n    `name`<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\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\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>\u5982\u4f55\u5206\u7ec4\u5e76\u884c\u5c55\u793a\u67e5\u8be2\u7ed3\u679c \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 \u89e3\u51b3\u65b9\u6848 \u5047\u8bbe\u8868\u7ed3\u6784\u5982\u4e0b\uff1a name start_date num ceshi 2021-11-22 1 sss 2021-11-21 2 ceshi 2021-01-20 3 \u76ee\u6807\u7ed3\u679c name start_date shang_sum xia_sum ceshi \u4e0a\u534a\u5e74 3 1 sss \u4e0b\u534a\u5e74 0 2 \u5b9e\u73b0\u4ee3\u7801 SELECT t.name, start_date, SUM( t.num ) AS shang_sum, IFNULL(t2.xia_sum,0) FROM `task` t LEFT JOIN ( SELECT NAME, SUM( num ) xia_sum FROM task [&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-6911","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6911","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=6911"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6911\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}