{"id":6266,"date":"2024-11-14T08:19:49","date_gmt":"2024-11-14T00:19:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6266\/"},"modified":"2024-11-14T08:19:49","modified_gmt":"2024-11-14T00:19:49","slug":"mysql%e6%80%8e%e4%b9%88%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6266\/","title":{"rendered":"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u63d2\u5165\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 insert \u8bed\u53e5\uff1a\u76f4\u63a5\u5411\u6307\u5b9a\u8868\u63d2\u5165\u4e00\u884c\u6216\u591a\u884c\u6570\u636e\u3002\u4f7f\u7528 insert into select \u8bed\u53e5\uff1a\u4ece\u4e00\u4e2a\u8868\u9009\u62e9\u6570\u636e\u5e76\u63d2\u5165\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u3002\u4f7f\u7528 load data infile \u8bed\u53e5\uff1a\u5feb\u901f\u5730\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u5230\u8868\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/20\/2024062013191440201.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u6dfb\u52a0\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5411 MySQL \u6570\u636e\u5e93\u4e2d\u63d2\u5165\u6570\u636e<\/strong><\/p>\n<p>MySQL \u4e2d\u5411\u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528 INSERT \u8bed\u53e5<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 INSERT \u8bed\u53e5\u3002INSERT \u8bed\u53e5\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>INSERT INTO table_name (column1, column2, ...)\nVALUES (value1, value2, ...);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>table_name \u662f\u8981\u5411\u5176\u4e2d\u63d2\u5165\u6570\u636e\u7684\u8868\u540d\u3002<\/li>\n<li>column1, column2\uff0c&#8230; \u662f\u8981\u63d2\u5165\u503c\u7684\u5217\u540d\u3002<\/li>\n<li>value1, value2\uff0c&#8230; \u662f\u8981\u63d2\u5165\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<p>\u8981\u5411 users \u8868\u4e2d\u63d2\u5165\u4e00\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b INSERT \u8bed\u53e5\uff1a<\/p>\n<pre>INSERT INTO users (name, email)\nVALUES ('John Doe', 'johndoe@example.com');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 INSERT INTO SELECT \u8bed\u53e5<\/strong><\/p>\n<p>INSERT INTO SELECT \u8bed\u53e5\u5141\u8bb8\u60a8\u4ece\u4e00\u4e2a\u8868\u5c06\u6570\u636e\u63d2\u5165\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>INSERT INTO table_name1 (column1, column2, ...)\nSELECT column1, column2, ...\nFROM table_name2\nWHERE condition;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>table_name1 \u662f\u8981\u63d2\u5165\u6570\u636e\u7684\u76ee\u6807\u8868\u3002<\/li>\n<li>column1, column2\uff0c&#8230; \u662f\u8981\u63d2\u5165\u7684\u5217\u540d\u3002<\/li>\n<li>table_name2 \u662f\u8981\u4ece\u5176\u9009\u62e9\u6570\u636e\u7684\u6e90\u8868\u3002<\/li>\n<li>condition \u662f\u4e00\u4e2a\u53ef\u9009\u7684\u6761\u4ef6\uff0c\u7528\u4e8e\u8fc7\u6ee4\u8981\u63d2\u5165\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<p>\u8981\u5c06 users \u8868\u4e2d\u7684\u6240\u6709\u7528\u6237\u63d2\u5165\u5230 customers \u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b INSERT INTO SELECT \u8bed\u53e5\uff1a<\/p>\n<pre>INSERT INTO customers (name, email)\nSELECT name, email\nFROM users;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 LOAD DATA INFILE \u8bed\u53e5<\/strong><\/p>\n<p>LOAD DATA INFILE \u8bed\u53e5\u5141\u8bb8\u60a8\u4ece\u6587\u4ef6\u5c06\u5927\u91cf\u6570\u636e\u5feb\u901f\u63d2\u5165\u5230\u8868\u4e2d\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>LOAD DATA INFILE 'filename.csv'\nINTO TABLE table_name\nFIELDS TERMINATED BY ','\nLINES TERMINATED BY '\n';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>filename.csv \u662f\u8981\u52a0\u8f7d\u7684\u6587\u4ef6\u540d\u3002<\/li>\n<li>table_name \u662f\u8981\u5c06\u6570\u636e\u63d2\u5165\u7684\u76ee\u6807\u8868\u3002<\/li>\n<li>FIELDS TERMINATED BY &#8216;,&#8217; \u6307\u5b9a\u5b57\u6bb5\u5206\u9694\u7b26\u3002<\/li>\n<li>LINES TERMINATED BY &#8216; &#8216; \u6307\u5b9a\u884c\u5206\u9694\u7b26\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5728\u63d2\u5165\u6570\u636e\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u62e5\u6709\u5728\u76ee\u6807\u8868\u4e2d\u63d2\u5165\u6570\u636e\u7684\u6743\u9650\u3002<\/li>\n<li>\u63d2\u5165\u7684\u6570\u636e\u5fc5\u987b\u4e0e\u76ee\u6807\u8868\u7684\u5217\u7c7b\u578b\u548c\u7ea6\u675f\u76f8\u5339\u914d\u3002<\/li>\n<li>\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u51fa\u73b0\u9519\u8bef\uff0cMySQL \u5c06\u4f1a\u8fd4\u56de\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u6dfb\u52a0\u6570\u636e\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>mysql \u4e2d\u63d2\u5165\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 insert \u8bed\u53e5\uff1a\u76f4\u63a5\u5411\u6307\u5b9a\u8868\u63d2\u5165\u4e00\u884c\u6216\u591a\u884c\u6570\u636e\u3002\u4f7f\u7528 insert into select \u8bed\u53e5\uff1a\u4ece\u4e00\u4e2a\u8868\u9009\u62e9\u6570\u636e\u5e76\u63d2\u5165\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u3002\u4f7f\u7528 load data infile \u8bed\u53e5\uff1a\u5feb\u901f\u5730\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u5230\u8868\u4e2d\u3002 \u5982\u4f55\u5411 MySQL \u6570\u636e\u5e93\u4e2d\u63d2\u5165\u6570\u636e MySQL \u4e2d\u5411\u6570\u636e\u5e93\u63d2\u5165\u6570\u636e\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002 \u4f7f\u7528 INSERT \u8bed\u53e5 \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 INSERT \u8bed\u53e5\u3002INSERT \u8bed\u53e5\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a INSERT INTO table_name (column1, column2, &#8230;) VALUES (value1, value2, &#8230;); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a table_name \u662f\u8981\u5411\u5176\u4e2d\u63d2\u5165\u6570\u636e\u7684\u8868\u540d\u3002 column1, column2\uff0c&#8230; \u662f\u8981\u63d2\u5165\u503c\u7684\u5217\u540d\u3002 value1, value2\uff0c&#8230; \u662f\u8981\u63d2\u5165\u7684\u503c\u3002 \u4f8b\u5982\uff1a \u8981\u5411 users \u8868\u4e2d\u63d2\u5165\u4e00\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b INSERT \u8bed\u53e5\uff1a INSERT INTO users (name, email) [&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-6266","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6266","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=6266"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6266\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}