{"id":6750,"date":"2024-11-14T08:57:38","date_gmt":"2024-11-14T00:57:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6750\/"},"modified":"2024-11-14T08:57:38","modified_gmt":"2024-11-14T00:57:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-sql-%e6%9f%a5%e8%af%a2%e6%af%8f%e4%b8%aa%e4%ba%a7%e5%93%81%e5%9c%a8%e7%89%b9%e5%ae%9a%e6%97%a5%e6%9c%9f%e7%9a%84%e9%94%80%e9%87%8f%e5%b9%b6%e6%8c%89%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6750\/","title":{"rendered":"\u5982\u4f55\u7528 SQL \u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\u5e76\u6309\u65e5\u671f\u6c47\u603b\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172986030320985.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528 SQL \u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\u5e76\u6309\u65e5\u671f\u6c47\u603b\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528 SQL \u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\u5e76\u6309\u65e5\u671f\u6c47\u603b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5173\u4e8e\u4e2d\u6570\u636e\u67e5\u8be2\u57fa\u7840\u95ee\u9898\u7684\u89e3\u7b54<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u6839\u636e\u7ed9\u5b9a\u8868\u683c\u4e2d\u7684\u6570\u636e\uff0c\u4f7f\u7528sql\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\uff0c\u5e76\u5c06\u5176\u6309\u65e5\u671f\u6c47\u603b\uff1f<\/p>\n<p><strong>\u793a\u4f8b\u8868\u683c\uff1a<\/strong><\/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>\u9884\u671f\u7ed3\u679c\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1<\/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>3<\/td>\n<td>7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u9519\u8bef\u4ee3\u7801\u5206\u6790\uff1a<\/strong><\/p>\n<p>\u63d0\u4f9b\u7684sql\u4ee3\u7801\u9519\u8bef\u5728\u4e8e\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2aleft join\uff0c\u800c\u8fd9\u5728\u6b64\u60c5\u51b5\u4e0b\u5e76\u4e0d\u9002\u7528\u3002left join\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u8868\uff0c\u5176\u4e2d\u4e00\u4e2a\u8868\u53ef\u4ee5\u5305\u542b\u53ef\u80fd\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u76f8\u5173\u8054\u7684\u8bb0\u5f55\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6ca1\u6709\u7406\u7531\u5c06\u4e24\u4e2a\u5b50\u67e5\u8be2\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p><strong>\u6b63\u786e\u7684sql\u67e5\u8be2\uff1a<\/strong><\/p>\n<pre>SELECT\n    \u4ea7\u54c1 AS \u4ea7\u54c1\u9500\u91cf,\n    SUM(CASE\n        WHEN \u65e5\u671f = '2021-1-1' THEN \u9500\u91cf\n        ELSE 0\n    END) AS '2021-1-1',\n    SUM(CASE\n        WHEN \u65e5\u671f = '2021-1-2' THEN \u9500\u91cf\n        ELSE 0\n    END) AS '2021-1-2'\nFROM\n    mytable\nGROUP BY\n    \u4ea7\u54c1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7528 SQL \u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\u5e76\u6309\u65e5\u671f\u6c47\u603b\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>\u5173\u4e8e\u4e2d\u6570\u636e\u67e5\u8be2\u57fa\u7840\u95ee\u9898\u7684\u89e3\u7b54 \u95ee\u9898\uff1a \u5982\u4f55\u6839\u636e\u7ed9\u5b9a\u8868\u683c\u4e2d\u7684\u6570\u636e\uff0c\u4f7f\u7528sql\u67e5\u8be2\u6bcf\u4e2a\u4ea7\u54c1\u5728\u7279\u5b9a\u65e5\u671f\u7684\u9500\u91cf\uff0c\u5e76\u5c06\u5176\u6309\u65e5\u671f\u6c47\u603b\uff1f \u793a\u4f8b\u8868\u683c\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 \u9884\u671f\u7ed3\u679c\uff1a \u4ea7\u54c1 2021-1-1 2021-1-2 a 5 3 b 3 7 \u9519\u8bef\u4ee3\u7801\u5206\u6790\uff1a \u63d0\u4f9b\u7684sql\u4ee3\u7801\u9519\u8bef\u5728\u4e8e\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2aleft join\uff0c\u800c\u8fd9\u5728\u6b64\u60c5\u51b5\u4e0b\u5e76\u4e0d\u9002\u7528\u3002left join\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u8868\uff0c\u5176\u4e2d\u4e00\u4e2a\u8868\u53ef\u4ee5\u5305\u542b\u53ef\u80fd\u4e0e\u53e6\u4e00\u4e2a\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\u76f8\u5173\u8054\u7684\u8bb0\u5f55\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6ca1\u6709\u7406\u7531\u5c06\u4e24\u4e2a\u5b50\u67e5\u8be2\u8fde\u63a5\u8d77\u6765\u3002 \u6b63\u786e\u7684sql\u67e5\u8be2\uff1a SELECT \u4ea7\u54c1 AS \u4ea7\u54c1\u9500\u91cf, SUM(CASE WHEN \u65e5\u671f = &#8216;2021-1-1&#8217; THEN \u9500\u91cf ELSE 0 END) AS [&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-6750","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6750","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=6750"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6750\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}