{"id":200558,"date":"2025-05-06T09:26:50","date_gmt":"2025-05-06T01:26:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200558\/"},"modified":"2025-05-06T09:26:50","modified_gmt":"2025-05-06T01:26:50","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-sql-%e6%9f%a5%e8%af%a2%e5%b0%86%e8%a1%a8%e4%b8%ad%e6%95%b0%e6%8d%ae%e5%88%86%e7%bb%84%e5%b9%b6%e5%b9%b3%e8%a1%8c%e5%b1%95%e7%a4%ba%e5%8d%8a%e5%b9%b4%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200558\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 SQL \u67e5\u8be2\u5c06\u8868\u4e2d\u6570\u636e\u5206\u7ec4\u5e76\u5e73\u884c\u5c55\u793a\u534a\u5e74\u7edf\u8ba1\u7ed3\u679c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5229\u7528 SQL \u67e5\u8be2\u5c06\u8868\u4e2d\u6570\u636e\u5206\u7ec4\u5e76\u5e73\u884c\u5c55\u793a\u534a\u5e74\u7edf\u8ba1\u7ed3\u679c\uff1f <\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731050496672dbc00d4634.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u9488\u5bf9\u8868\u4e2d\u7684\u5206\u7ec4\u67e5\u8be2\u5e76\u884c\u5c55\u793a\u7ed3\u679c<\/strong><\/p>\n<p>\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5904\u7406\u8868\u4e2d\u6570\u636e\u5206\u7ec4\u5e76\u8fdb\u884c\u5e73\u884c\u5c55\u793a\u3002\u8003\u8651\u5982\u4e0b\u8868\u7ed3\u6784\uff1a<\/p>\n<pre>create table task (\n  `name` varchar(255) not null,\n  `start_date` date not null,\n  `num` int not null\n);<\/pre>\n<p>\u5047\u8bbe\u8868\u4e2d\u6570\u636e\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>\u76ee\u6807\u662f\u6839\u636e name \u548c start_date \u5206\u7ec4\uff0c\u5e76\u663e\u793a\u534a\u5e74\u7edf\u8ba1\u6570\u636e\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>\u7ecf\u8fc7\u5c1d\u8bd5\uff0c\u4ee5\u4e0b sql \u67e5\u8be2\u65e0\u6cd5\u5b9e\u73b0\u9884\u671f\u6548\u679c\uff1a<\/p>\n<pre>select \n    `name`,\n    start_date,\n    if(month(start_date) &gt;= 7,'\u4e0b\u534a\u5e74','\u4e0a\u534a\u5e74') as cycle,\n    if(if(month(start_date) &gt;= 7,'\u4e0b\u534a\u5e74','\u4e0a\u534a\u5e74') = '\u4e0a\u534a\u5e74',sum(num),0) as shang_sum,\n    if(if(month(start_date) &gt;= 7,'\u4e0b\u534a\u5e74','\u4e0a\u534a\u5e74') = '\u4e0b\u534a\u5e74',sum(num),0) as xia_sum\nfrom \n    `task` \ngroup by `name`,cycle<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u66ff\u4ee3\u65b9\u6848\u662f\u4f7f\u7528 left join\uff0c\u4ee5\u4ee5\u4e0b sql \u67e5\u8be2\u4e3a\u4f8b\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>\u6b64\u67e5\u8be2\u5c06\u8f93\u51fa\u9884\u671f\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\uff1a<\/p>\n<ul>\n<li>left join \u4f1a\u5c06\u6ca1\u6709\u5339\u914d\u8bb0\u5f55\u7684\u8bb0\u5f55\u8fde\u63a5\u5230\u7ed3\u679c\u96c6\u4e2d\uff0c\u5e76\u4f7f\u7528 ifnull() \u51fd\u6570\u586b\u5145 null \u503c\u4e3a 0\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u8ba1\u7b97\u5728\u4e0b\u534a\u5e74\u4e4b\u524d\u8bb0\u5f55\u7684 num \u603b\u548c\u3002<\/li>\n<li>where \u5b50\u53e5\u4ece task \u8868\u4e2d\u8fc7\u6ee4\u51fa\u4e0a\u534a\u5e74\u7684\u8bb0\u5f55\u3002<\/li>\n<li>\u5206\u7ec4\u6839\u636e name \u8fdb\u884c\uff0c\u5e76\u6c47\u603b num \u8ba1\u7b97\u4e0a\u534a\u5e74\u7684\u603b\u548c\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5229\u7528 SQL \u67e5\u8be2\u5c06\u8868\u4e2d\u6570\u636e\u5206\u7ec4\u5e76\u5e73\u884c\u5c55\u793a\u534a\u5e74\u7edf\u8ba1\u7ed3\u679c\uff1f \u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528 SQL \u67e5\u8be2\u5c06\u8868\u4e2d\u6570\u636e&#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-200558","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200558","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=200558"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200558\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}