{"id":6762,"date":"2024-11-14T09:56:40","date_gmt":"2024-11-14T01:56:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6762\/"},"modified":"2024-11-14T09:56:40","modified_gmt":"2024-11-14T01:56:40","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:\/\/fwq.ai\/blog\/6762\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172990783231671.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u636e\u67e5\u8be2\u57fa\u7840\uff1a\u6309\u65e5\u671f\u5206\u7ec4\u6c42\u548c<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u5229\u7528\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u5982\u4f55\u4f7f\u7528SQL\u8bed\u53e5\u7edf\u8ba1\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\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>\u6570\u636e\u67e5\u8be2\u57fa\u7840\uff1a\u6309\u65e5\u671f\u5206\u7ec4\u6c42\u548c \u95ee\u9898\uff1a\u5982\u4f55\u5229\u7528\u4ece\u7ed9\u5b9a\u8868\u683c\u4e2d\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u7684\u65e5\u9500\u552e\u91cf\uff1f \u8868\u683c\u7ed3\u6784\u5982\u4e0b\uff1a \u65e5\u671f \u4ea7\u54c1 \u9500\u91cf 2021-1-1 a 3 2021-1-1 b 7 2021-1-1 a 2 2021-1-2 b 1 2021-1-2 a 3 2021-1-2 b 6 sql\u8bed\u53e5\u793a\u4f8b\uff1a SELECT \u4ea7\u54c1 AS \u4ea7\u54c1\u9500\u91cf, SUM(CASE \u65e5\u671f WHEN &#8216;2021-1-1&#8217; THEN \u9500\u91cf ELSE 0 END) AS &#8216;2021-1-1&#8217;, SUM(CASE \u65e5\u671f WHEN &#8216;2021-1-2&#8217; THEN \u9500\u91cf ELSE 0 END) AS &#8216;2021-1-2&#8217; FROM mytable GROUP BY [&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-6762","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6762","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=6762"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6762\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}