{"id":6344,"date":"2024-11-14T09:44:36","date_gmt":"2024-11-14T01:44:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6344\/"},"modified":"2024-11-14T09:44:36","modified_gmt":"2024-11-14T01:44:36","slug":"mysql%e8%a1%8c%e5%88%97%e8%bd%ac%e6%8d%a2%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6344\/","title":{"rendered":"mysql\u884c\u5217\u8f6c\u6362\u51fd\u6570"},"content":{"rendered":"<blockquote><p>\n  \u6458\u8981\uff1a mysql\u884c\u5217\u8f6c\u6362\u51fd\u6570\u63d0\u4f9b\u65b9\u6cd5\u5728\u884c\u548c\u5217\u4e4b\u95f4\u8f6c\u6362\u6570\u636e\uff0c\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u62a5\u8868\u751f\u6210\u3002\u5176\u4e3b\u8981\u51fd\u6570\u5305\u62ec\uff1apivot\uff1a \u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff0c\u8bed\u6cd5\uff1apivot ( sum(value_to_sum) for column_to_group_by in (value_1, value_2, &#8230;))unpivot\uff1a \u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\uff0c\u8bed\u6cd5\uff1aunpivot include nulls (value for column_to_unpivot in (value_1, value_2, &#8230;))\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080102365138946.jpg\" class=\"aligncenter\" title=\"mysql\u884c\u5217\u8f6c\u6362\u51fd\u6570\u63d2\u56fe\" alt=\"mysql\u884c\u5217\u8f6c\u6362\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u884c\u5217\u8f6c\u6362\u51fd\u6570<\/strong><\/p>\n<p>\u884c\u5217\u8f6c\u6362\u51fd\u6570\u7528\u4e8e\u5728\u884c\u548c\u5217\u4e4b\u95f4\u8f6c\u6362\u6570\u636e\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c\u62a5\u8868\u751f\u6210\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u6700\u5e38\u7528\u7684\u884c\u5217\u8f6c\u6362\u51fd\u6570\u5305\u62ec\uff1a<\/strong><\/p>\n<p><strong>1. PIVOT\uff1a<\/strong><\/p>\n<ul>\n<li>\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\u3002<\/li>\n<li>\n<p>\u8bed\u6cd5\uff1a`sql<br \/>SELECT [aggregate_function](column_to_pivot)<br \/>FROM table_name<br \/>PIVOT (<br \/> SUM(value_to_sum)<br \/> FOR column_to_group_by IN (value_1, value_2, &#8230;)<br \/>)<br \/>ORDER BY [order_by_clause];<\/p>\n<pre>\n**2. UNPIVOT\uff1a**<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\u3002<\/li>\n<li>\n<p>\u8bed\u6cd5\uff1a`sql<br \/>SELECT column_to_unpivot, value<br \/>FROM table_name<br \/>UNPIVOT INCLUDE NULLS (value FOR column_to_unpivot IN (value_1, value_2, &#8230;))<br \/>ORDER BY [order_by_clause];<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b 1\uff1a\u4f7f\u7528 PIVOT \u5c06\u9500\u552e\u6570\u636e\u8f6c\u6362\u4e3a\u6309\u4ea7\u54c1\u5206\u7ec4\u7684\u5217<\/strong><\/p>\n<pre>SELECT Product, \n       SUM(Sales) AS TotalSales\nFROM SalesData\nPIVOT (\n    SUM(Sales)\n    FOR Product IN (Product1, Product2, Product3)\n)\nORDER BY TotalSales DESC;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b 2\uff1a\u4f7f\u7528 UNPIVOT \u5c06\u5b66\u751f\u6210\u7ee9\u8f6c\u6362\u4e3a\u6309\u5b66\u751f\u5206\u7ec4\u7684\u884c<\/strong><\/p>\n<pre>SELECT StudentName, Subject, Grade\nFROM Grades\nUNPIVOT INCLUDE NULLS (Grade FOR Subject IN (Math, Science, History))\nWHERE Grade IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u884c\u5217\u8f6c\u6362\u51fd\u6570\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>\u6458\u8981\uff1a mysql\u884c\u5217\u8f6c\u6362\u51fd\u6570\u63d0\u4f9b\u65b9\u6cd5\u5728\u884c\u548c\u5217\u4e4b\u95f4\u8f6c\u6362\u6570\u636e\uff0c\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u62a5\u8868\u751f\u6210\u3002\u5176\u4e3b\u8981\u51fd\u6570\u5305\u62ec\uff1apivot\uff1a \u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff0c\u8bed\u6cd5\uff1apivot ( sum(value_to_sum) for column_to_group_by in (value_1, value_2, &#8230;))unpivot\uff1a \u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\uff0c\u8bed\u6cd5\uff1aunpivot include nulls (value for column_to_unpivot in (value_1, value_2, &#8230;)) MySQL \u884c\u5217\u8f6c\u6362\u51fd\u6570 \u884c\u5217\u8f6c\u6362\u51fd\u6570\u7528\u4e8e\u5728\u884c\u548c\u5217\u4e4b\u95f4\u8f6c\u6362\u6570\u636e\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c\u62a5\u8868\u751f\u6210\u4e2d\u975e\u5e38\u6709\u7528\u3002 \u6700\u5e38\u7528\u7684\u884c\u5217\u8f6c\u6362\u51fd\u6570\u5305\u62ec\uff1a 1. PIVOT\uff1a \u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\u3002 \u8bed\u6cd5\uff1a`sqlSELECT [aggregate_function](column_to_pivot)FROM table_namePIVOT ( SUM(value_to_sum) FOR column_to_group_by IN (value_1, value_2, &#8230;))ORDER BY [order_by_clause]; **2. UNPIVOT\uff1a** \u767b\u5f55\u540e\u590d\u5236 \u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\u3002 \u8bed\u6cd5\uff1a`sqlSELECT column_to_unpivot, valueFROM table_nameUNPIVOT INCLUDE NULLS (value FOR column_to_unpivot IN [&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-6344","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6344","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=6344"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6344\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}