{"id":5958,"date":"2024-11-14T09:55:47","date_gmt":"2024-11-14T01:55:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5958\/"},"modified":"2024-11-14T09:55:47","modified_gmt":"2024-11-14T01:55:47","slug":"sql%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%88%97%e8%bd%ac%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5958\/","title":{"rendered":"sql\u4e2d\u5982\u4f55\u5b9e\u73b0\u5217\u8f6c\u884c"},"content":{"rendered":"<blockquote><p>\n  sql\u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u64cd\u4f5c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528 union \u64cd\u4f5c\u7b26\u901a\u8fc7\u5782\u76f4\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5b9e\u73b0\u5217\u8f6c\u884c\u3002\u4f7f\u7528 pivot \u51fd\u6570\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\uff0c\u5176\u4e2d pivot \u51fd\u6570\u7684\u8bed\u6cd5\u4e3a\uff1apivot ( aggregate_function(column_name) for pivot_column_name in (value1, value2, &#8230;) ) from table_name\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/06\/2024060609362541439.jpg\" class=\"aligncenter\" title=\"sql\u4e2d\u5982\u4f55\u5b9e\u73b0\u5217\u8f6c\u884c\u63d2\u56fe\" alt=\"sql\u4e2d\u5982\u4f55\u5b9e\u73b0\u5217\u8f6c\u884c\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u64cd\u4f5c<\/strong><\/p>\n<p>\u5217\u8f6c\u884c\u64cd\u4f5c\uff0c\u4e5f\u79f0\u4e3a\u201c\u89e3\u6784\u201d\u6216\u201c\u4ea4\u53c9\u8868\u201d\uff0c\u662f\u6307\u5c06\u8868\u4e2d\u7684\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u884c\u6570\u636e\u3002\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 UNION \u64cd\u4f5c\u7b26\u6216 PIVOT \u51fd\u6570\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f7f\u7528 UNION \u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>UNION \u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5782\u76f4\u5408\u5e76\uff0c\u4ece\u800c\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>SELECT * FROM table_name\nUNION\nSELECT * FROM table_name\nWHERE column_name1 IS NOT NULL\nUNION\nSELECT * FROM table_name\nWHERE column_name2 IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 PIVOT \u51fd\u6570<\/strong><\/p>\n<p>PIVOT \u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u8f6c\u6362\u6570\u636e\u5e03\u5c40\uff0c\u53ef\u4ee5\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>PIVOT (\n  aggregate_function(column_name)\n  FOR pivot_column_name IN (value1, value2, ...)\n)\nFROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>aggregate_function: \u8981\u5e94\u7528\u4e8e\u8f6c\u6362\u5217\u6570\u636e\u7684\uff0c\u5982 SUM\u3001COUNT \u6216 MAX\u3002<\/li>\n<li>pivot_column_name: \u7528\u4f5c\u884c\u6807\u5934\u7684\u5217\u540d\u79f0\u3002<\/li>\n<li>value1, value2, &#8230;: \u6307\u5b9a\u8981\u8f6c\u884c\u7684\u5217\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a orders \u7684\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u5217\uff1a<\/p>\n<ul>\n<li>order_id<\/li>\n<li>product_name<\/li>\n<li>quantity<\/li>\n<li>sale_date<\/li>\n<\/ul>\n<p>\u8981\u5c06 product_name \u5217\u8f6c\u884c\uff0c\u4f7f\u7528 PIVOT \u51fd\u6570\uff1a<\/p>\n<pre>SELECT\n  order_id,\n  sale_date,\n  SUM(quantity) AS quantity\nFROM orders\nPIVOT (\n  SUM(quantity)\n  FOR product_name IN ('Product A', 'Product B', 'Product C')\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>order_id<\/th>\n<th>sale_date<\/th>\n<th>quantity_Product A<\/th>\n<th>quantity_Product B<\/th>\n<th>quantity_Product C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>2023-01-01<\/td>\n<td>10<\/td>\n<td>15<\/td>\n<td>20<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2023-01-02<\/td>\n<td>5<\/td>\n<td>10<\/td>\n<td>15<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u4e2d\u5982\u4f55\u5b9e\u73b0\u5217\u8f6c\u884c\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>sql\u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u64cd\u4f5c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528 union \u64cd\u4f5c\u7b26\u901a\u8fc7\u5782\u76f4\u5408\u5e76\u67e5\u8be2\u7ed3\u679c\u5b9e\u73b0\u5217\u8f6c\u884c\u3002\u4f7f\u7528 pivot \u51fd\u6570\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u6570\u636e\uff0c\u5176\u4e2d pivot \u51fd\u6570\u7684\u8bed\u6cd5\u4e3a\uff1apivot ( aggregate_function(column_name) for pivot_column_name in (value1, value2, &#8230;) ) from table_name SQL \u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u64cd\u4f5c \u5217\u8f6c\u884c\u64cd\u4f5c\uff0c\u4e5f\u79f0\u4e3a\u201c\u89e3\u6784\u201d\u6216\u201c\u4ea4\u53c9\u8868\u201d\uff0c\u662f\u6307\u5c06\u8868\u4e2d\u7684\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u884c\u6570\u636e\u3002\u5728 SQL \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 UNION \u64cd\u4f5c\u7b26\u6216 PIVOT \u51fd\u6570\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002 \u4f7f\u7528 UNION \u64cd\u4f5c\u7b26 UNION \u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u591a\u4e2a\u67e5\u8be2\u7ed3\u679c\u5782\u76f4\u5408\u5e76\uff0c\u4ece\u800c\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a SELECT * FROM table_name UNION SELECT * FROM table_name WHERE column_name1 IS NOT NULL UNION SELECT * FROM table_name WHERE column_name2 IS NOT [&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-5958","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5958","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=5958"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5958\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}