{"id":6914,"date":"2024-11-14T14:10:16","date_gmt":"2024-11-14T06:10:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6914\/"},"modified":"2024-11-14T14:10:16","modified_gmt":"2024-11-14T06:10:16","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-mysql-%e5%88%86%e7%bb%84%e5%b1%95%e7%a4%ba%e6%9f%a5%e8%af%a2%e7%bb%93%e6%9e%9c%e5%b9%b6%e8%ae%a1%e7%ae%97%e6%af%8f%e4%b8%aa%e5%88%86%e7%bb%84%e7%9a%84%e8%81%9a","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6914\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 MySQL \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u805a\u5408\u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173007721212858.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 MySQL \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u805a\u5408\u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 MySQL \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u805a\u5408\u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5229\u7528  \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\uff0c\u8981\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\u5e76\u884c\u5c55\u793a\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5148\u6309\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u4f8b\u5982\u6309 name \u548c start_date \u5206\u7ec4\uff1a<\/p>\n<pre>select \n    `name`, \n    start_date\nfrom \n    `task`\ngroup by \n    `name`, \n    start_date;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5206\u7ec4\uff0c\u5206\u522b\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u5185\u6bcf\u4e2a\u5b57\u6bb5\u7684\u805a\u5408\u503c\uff0c\u4f8b\u5982\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u7684 num \u5b57\u6bb5\u7684\u603b\u548c\uff1a<\/p>\n<pre>select \n    `name`,\n    start_date,\n    sum(`num`) as `sum_num`\nfrom \n    `task`\ngroup by \n    `name`, \n    start_date;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u518d\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u9020\u989d\u5916\u7684\u5b57\u6bb5\u6765\u5c55\u793a\u60f3\u8981\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e start_date \u5b57\u6bb5\u5c06\u6570\u636e\u5206\u4e3a &#8220;\u4e0a\u534a\u5e74&#8221; \u548c &#8220;\u4e0b\u534a\u5e74&#8221;\uff1a<\/p>\n<pre>select \n    `name`,\n    start_date,\n    sum(`num`) as `sum_num`,\n    if(month(start_date) &gt;= 7, '\u4e0b\u534a\u5e74', '\u4e0a\u534a\u5e74') as `cycle`\nfrom \n    `task`\ngroup by \n    `name`, \n    `cycle`;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 case \u8bed\u53e5\u6216 ifnull \u51fd\u6570\u6765\u5904\u7406\u7a7a\u503c\uff0c\u5e76\u5c06\u7ed3\u679c\u5c55\u793a\u6210\u9700\u8981\u7684\u683c\u5f0f\uff1a<\/p>\n<pre>SELECT \n    `name`,\n    '\u4e0a\u534a\u5e74' AS `start_date`,\n    SUM(`num`) AS `shang_sum`,\n    IFNULL(t2.`xia_sum`,0) AS `xia_sum`\nFROM \n    `task` t\nLEFT JOIN ( \n    SELECT \n        `name`, \n        SUM(`num`) AS `xia_sum` \n    FROM \n        `task` \n    WHERE \n        MONTH(start_date) &lt; 7 \n    GROUP BY \n        `name` \n) 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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 MySQL \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u805a\u5408\u503c\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\u5229\u7528 \u5206\u7ec4\u5c55\u793a\u67e5\u8be2\u7ed3\u679c \u5728 mysql \u4e2d\uff0c\u8981\u5c06\u67e5\u8be2\u7ed3\u679c\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\u5e76\u884c\u5c55\u793a\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u9996\u5148\uff0c\u5148\u6309\u6307\u5b9a\u7684\u5b57\u6bb5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u4f8b\u5982\u6309 name \u548c start_date \u5206\u7ec4\uff1a select `name`, start_date from `task` group by `name`, start_date; \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5206\u7ec4\uff0c\u5206\u522b\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u5185\u6bcf\u4e2a\u5b57\u6bb5\u7684\u805a\u5408\u503c\uff0c\u4f8b\u5982\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u7684 num \u5b57\u6bb5\u7684\u603b\u548c\uff1a select `name`, start_date, sum(`num`) as `sum_num` from `task` group by `name`, start_date; \u767b\u5f55\u540e\u590d\u5236 \u518d\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u9020\u989d\u5916\u7684\u5b57\u6bb5\u6765\u5c55\u793a\u60f3\u8981\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e start_date \u5b57\u6bb5\u5c06\u6570\u636e\u5206\u4e3a &#8220;\u4e0a\u534a\u5e74&#8221; \u548c &#8220;\u4e0b\u534a\u5e74&#8221;\uff1a select `name`, start_date, sum(`num`) as `sum_num`, if(month(start_date) &gt;= 7, &#8216;\u4e0b\u534a\u5e74&#8217;, &#8216;\u4e0a\u534a\u5e74&#8217;) as `cycle` [&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-6914","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6914","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=6914"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6914\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}