{"id":200203,"date":"2025-05-06T12:19:20","date_gmt":"2025-05-06T04:19:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200203\/"},"modified":"2025-05-06T12:19:20","modified_gmt":"2025-05-06T04:19:20","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sql%e8%af%ad%e5%8f%a5%e7%bb%9f%e8%ae%a1%e6%af%8f%e4%b8%aa%e4%ba%a7%e5%93%81%e7%9a%84%e6%97%a5%e9%94%80%e5%94%ae%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200203\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f <\/h1>\n<p>\u6570\u636e\u5e93\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f \u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730093762671f22c28a877.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql\u6570\u636e\u67e5\u8be2\u57fa\u7840\uff1a\u6309\u65e5\u671f\u5206\u7ec4\u6c42\u548c<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u5229\u7528sql\u8bed\u53e5\u4ece\u7ed9\u5b9a\u8868\u683c\u4e2d\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f<\/p>\n<p>\u8868\u683c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u65e5\u671f<\/th>\n<th>\u4ea7\u54c1<\/th>\n<th>\u9500\u91cf<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>2021-1-1<\/td>\n<td>a<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>2021-1-1<\/td>\n<td>b<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<td>2021-1-1<\/td>\n<td>a<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>2021-1-2<\/td>\n<td>b<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2021-1-2<\/td>\n<td>a<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>2021-1-2<\/td>\n<td>b<\/td>\n<td>6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>sql\u8bed\u53e5\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT \u4ea7\u54c1 AS \u4ea7\u54c1\u9500\u91cf,\nSUM(CASE \u65e5\u671f WHEN '2021-1-1' THEN \u9500\u91cf ELSE 0 END) AS '2021-1-1',\nSUM(CASE \u65e5\u671f WHEN '2021-1-2' THEN \u9500\u91cf ELSE 0 END) AS '2021-1-2'\nFROM mytable\nGROUP BY \u4ea7\u54c1;<\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1\u9500\u91cf<\/th>\n<th>2021-1-1<\/th>\n<th>2021-1-2<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>a<\/td>\n<td>5<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>b<\/td>\n<td>7<\/td>\n<td>7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u89e3\u6790\uff1a<\/strong><\/p>\n<ul>\n<li>sum(case \u65e5\u671f when &#8216;2021-1-1&#8217; then \u9500\u91cf else 0 end)\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u76842021-1-1\u7684\u9500\u552e\u91cf\uff0c\u5982\u679c\u65e5\u671f\u4e0d\u7b49\u4e8e2021-1-1\uff0c\u5219\u8fd4\u56de0\u3002<\/li>\n<li>sum(case \u65e5\u671f when &#8216;2021-1-2&#8217; then \u9500\u91cf else 0 end)\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u76842021-1-2\u7684\u9500\u552e\u91cf\uff0c\u5982\u679c\u65e5\u671f\u4e0d\u7b49\u4e8e2021-1-2\uff0c\u5219\u8fd4\u56de0\u3002<\/li>\n<li>group by \u4ea7\u54c1\uff1a\u6309\u4ea7\u54c1\u5206\u7ec4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\u6c42\u548c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684&#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-200203","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200203","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=200203"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200203\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}