{"id":48641,"date":"2024-12-02T12:26:39","date_gmt":"2024-12-02T04:26:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48641\/"},"modified":"2024-12-02T12:26:39","modified_gmt":"2024-12-02T04:26:39","slug":"mysql-%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%8a%e5%8d%8a%e5%b9%b4%e5%92%8c%e4%b8%8b%e5%8d%8a%e5%b9%b4%e5%88%86%e7%bb%84%e6%95%b0%e6%8d%ae%e5%b9%b6%e6%8e%92%e5%b1%95%e7%a4%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48641\/","title":{"rendered":"MySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>MySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aMySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/173017912767207037ea8bf.jpg\" class=\"aligncenter\" title=\"MySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f\u63d2\u56fe\" alt=\"MySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>mysql \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u672c\u6587\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528 mysql \u67e5\u8be2\u5c06\u6570\u636e\u96c6\u5206\u7ec4\u5e76\u5e76\u884c\u5c55\u793a\u4e0d\u540c\u7ec4\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u5982\u4e0b\u8868\u7ed3\u6784\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>\u76ee\u6807\u662f\u6839\u636e name \u548c start_date \u5206\u7ec4\uff0c\u5e76\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u7684\u7edf\u8ba1\u4fe1\u606f\u5e76\u6392\u663e\u793a\u5728\u540c\u4e00\u884c\u4e2d\u3002\u671f\u671b\u7ed3\u679c\u4e3a\uff1a<\/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>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u6765\u5b9e\u73b0\uff1a<\/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>\u67e5\u8be2\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 left join \u8fde\u63a5 task \u8868\u548c\u4e00\u4e2a\u5b50\u67e5\u8be2 t2\uff0c\u540e\u8005\u5bf9\u4e0b\u534a\u5e74\u7684\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u5e76\u6c42\u548c\u3002<\/li>\n<li>\u4f7f\u7528 sum() \u805a\u5408\u51fd\u6570\u5bf9 num \u5b57\u6bb5\u8fdb\u884c\u6c42\u548c\uff0c\u4ee5\u83b7\u53d6\u4e0a\u534a\u5e74\u7684\u603b\u548c (shang_sum) \u548c\u4e0b\u534a\u5e74\u7684\u603b\u548c (xia_sum)\u3002<\/li>\n<li>\u8fc7\u6ee4\u7ed3\u679c\uff0c\u53ea\u9009\u62e9\u4e0a\u534a\u5e74\u7684\u8bb0\u5f55\u3002<\/li>\n<li>\u4f7f\u7528 group by \u6839\u636e name \u5206\u7ec4\uff0c\u5e76\u5c06 shang_sum \u548c xia_sum \u6c47\u603b\u5230\u540c\u4e00\u884c\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aMySQL \u5982\u4f55\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u5206\u7ec4\u6570\u636e\u5e76\u6392\u5c55\u793a\uff1f\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 mysql \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c \u672c\u6587\u5c06\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528 mysql \u67e5\u8be2\u5c06\u6570\u636e\u96c6\u5206\u7ec4\u5e76\u5e76\u884c\u5c55\u793a\u4e0d\u540c\u7ec4\u7684\u7ed3\u679c\u3002 \u5047\u8bbe\u6709\u5982\u4e0b\u8868\u7ed3\u6784\uff1a name start_date num ceshi 2021-11-22 1 sss 2021-11-21 2 ceshi 2021-01-20 3 \u76ee\u6807\u662f\u6839\u636e name \u548c start_date \u5206\u7ec4\uff0c\u5e76\u5c06\u4e0a\u534a\u5e74\u548c\u4e0b\u534a\u5e74\u7684\u7edf\u8ba1\u4fe1\u606f\u5e76\u6392\u663e\u793a\u5728\u540c\u4e00\u884c\u4e2d\u3002\u671f\u671b\u7ed3\u679c\u4e3a\uff1a name start_date shang_sum xia_sum ceshi \u4e0a\u534a\u5e74 3 1 sss \u4e0b\u534a\u5e74 0 2 \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\u6765\u5b9e\u73b0\uff1a SELECT t.name, start_date, SUM( t.num ) AS shang_sum, IFNULL(t2.xia_sum,0) FROM `task` t LEFT JOIN ( [&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-48641","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48641","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=48641"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48641\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}