{"id":5265,"date":"2024-11-14T12:40:55","date_gmt":"2024-11-14T04:40:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5265\/"},"modified":"2024-11-14T12:40:55","modified_gmt":"2024-11-14T04:40:55","slug":"mysql%e4%b8%ad%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%88%97%e8%bd%ac%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5265\/","title":{"rendered":"mysql\u4e2d\u600e\u4e48\u5b9e\u73b0\u5217\u8f6c\u884c"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\uff0c\u5b9e\u73b0\u5217\u8f6c\u884c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 unpivot \u8fd0\u7b97\u7b26\u5c06\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u6570\u636e\uff0c\u6bcf\u4e00\u884c\u5305\u542b\u4e00\u4e2a\u5217\u540d\u548c\u4e00\u4e2a\u503c\u3002\u4f7f\u7528 case \u8868\u8fbe\u5f0f\u5c06\u6bcf\u4e2a\u5217\u503c\u4f5c\u4e3a case \u8bed\u53e5\u4e2d\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u5c06 case \u8868\u8fbe\u5f0f\u7ed3\u679c\u5206\u914d\u7ed9\u65b0\u5217\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052618004133500.jpg\" class=\"aligncenter\" title=\"mysql\u4e2d\u600e\u4e48\u5b9e\u73b0\u5217\u8f6c\u884c\u63d2\u56fe\" alt=\"mysql\u4e2d\u600e\u4e48\u5b9e\u73b0\u5217\u8f6c\u884c\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u4e24\u79cd\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u5c06\u5217\u8f6c\u884c\u4e3a\u884c\u7684\u64cd\u4f5c\u901a\u5e38\u79f0\u4e3a &#8220;\u89e3\u65cb&#8221;\u3002\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff1a<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\uff1aUNPIVOT \u8fd0\u7b97\u7b26<\/strong><\/p>\n<p>UNPIVOT \u8fd0\u7b97\u7b26\u7528\u4e8e\u5c06\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u6570\u636e\uff0c\u6bcf\u4e00\u884c\u5305\u542b\u4e00\u4e2a\u5217\u540d\u548c\u4e00\u4e2a\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT col1, val\nFROM table_name\nUNPIVOT (val FOR col IN (col2, col3, ..., coln))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u8868 sales \u4e2d\u7684 product_id\u3001product_name\u3001sales_amount\u3001sales_date \u56db\u5217\u89e3\u65cb\u4e3a\u4e24\u5217\uff1a<\/p>\n<pre>SELECT product_id, val\nFROM sales\nUNPIVOT (val FOR col IN (product_name, sales_amount, sales_date))<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1aCASE \u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>CASE \u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5217\u8f6c\u884c\u4e3a\uff0c\u901a\u8fc7\u5c06\u6bcf\u4e2a\u5217\u503c\u4f5c\u4e3a CASE \u8bed\u53e5\u4e2d\u7684\u4e00\u4e2a\u5206\u652f\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT\n  CASE \n    WHEN col1 = 'value1' THEN val1\n    WHEN col1 = 'value2' THEN val2\n    ...\n    ELSE NULL\n  END AS new_column,\n  col2,\n  col3,\n  ...\nFROM table_name<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u8868 sales \u4e2d\u7684 product_id\u3001product_name\u3001sales_amount \u4e09\u5217\u89e3\u65cb\u4e3a product \u548c value \u4e24\u5217\uff1a<\/p>\n<pre>SELECT\n  CASE \n    WHEN product_name IS NOT NULL THEN product_name\n    WHEN sales_amount IS NOT NULL THEN sales_amount\n    ELSE NULL\n  END AS product,\n  product_id,\n  sales_amount\nFROM sales<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2d\u600e\u4e48\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>\u5728 mysql \u4e2d\uff0c\u5b9e\u73b0\u5217\u8f6c\u884c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 unpivot \u8fd0\u7b97\u7b26\u5c06\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u6570\u636e\uff0c\u6bcf\u4e00\u884c\u5305\u542b\u4e00\u4e2a\u5217\u540d\u548c\u4e00\u4e2a\u503c\u3002\u4f7f\u7528 case \u8868\u8fbe\u5f0f\u5c06\u6bcf\u4e2a\u5217\u503c\u4f5c\u4e3a case \u8bed\u53e5\u4e2d\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u5c06 case \u8868\u8fbe\u5f0f\u7ed3\u679c\u5206\u914d\u7ed9\u65b0\u5217\u3002 MySQL \u4e2d\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u4e24\u79cd\u65b9\u6cd5 \u5728 MySQL \u4e2d\uff0c\u5c06\u5217\u8f6c\u884c\u4e3a\u884c\u7684\u64cd\u4f5c\u901a\u5e38\u79f0\u4e3a &#8220;\u89e3\u65cb&#8221;\u3002\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff1a \u65b9\u6cd5\u4e00\uff1aUNPIVOT \u8fd0\u7b97\u7b26 UNPIVOT \u8fd0\u7b97\u7b26\u7528\u4e8e\u5c06\u591a\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u5355\u5217\u6570\u636e\uff0c\u6bcf\u4e00\u884c\u5305\u542b\u4e00\u4e2a\u5217\u540d\u548c\u4e00\u4e2a\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT col1, val FROM table_name UNPIVOT (val FOR col IN (col2, col3, &#8230;, coln)) \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u8868 sales \u4e2d\u7684 product_id\u3001product_name\u3001sales_amount\u3001sales_date \u56db\u5217\u89e3\u65cb\u4e3a\u4e24\u5217\uff1a SELECT product_id, val FROM sales UNPIVOT (val FOR col IN (product_name, sales_amount, sales_date)) \u767b\u5f55\u540e\u590d\u5236 [&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-5265","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5265","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=5265"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5265\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}