{"id":6904,"date":"2024-11-14T09:53:03","date_gmt":"2024-11-14T01:53:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6904\/"},"modified":"2024-11-14T09:53:03","modified_gmt":"2024-11-14T01:53:03","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:\/\/fwq.ai\/blog\/6904\/","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><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173007415251752.jpg\" class=\"aligncenter\" title=\"\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\u63d2\u56fe\" alt=\"\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\u63d2\u56fe\" \/><\/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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>\u4ee5\u4e0a\u5c31\u662f\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\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\u9488\u5bf9\u8868\u4e2d\u7684\u5206\u7ec4\u67e5\u8be2\u5e76\u884c\u5c55\u793a\u7ed3\u679c \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 create table task ( `name` varchar(255) not null, `start_date` date not null, `num` int not null ); \u767b\u5f55\u540e\u590d\u5236 \u5047\u8bbe\u8868\u4e2d\u6570\u636e\u5982\u4e0b\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\u663e\u793a\u534a\u5e74\u7edf\u8ba1\u6570\u636e\uff1a name start_date shang_sum xia_sum ceshi \u4e0a\u534a\u5e74 3 1 sss \u4e0b\u534a\u5e74 0 2 \u7ecf\u8fc7\u5c1d\u8bd5\uff0c\u4ee5\u4e0b sql \u67e5\u8be2\u65e0\u6cd5\u5b9e\u73b0\u9884\u671f\u6548\u679c\uff1a select `name`, [&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-6904","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6904","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=6904"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6904\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}