{"id":200144,"date":"2025-05-06T10:10:57","date_gmt":"2025-05-06T02:10:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200144\/"},"modified":"2025-05-06T10:10:57","modified_gmt":"2025-05-06T02:10:57","slug":"%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e4%b8%8a%e4%bc%a0csv%e6%96%87%e4%bb%b6%e6%95%b0%e6%8d%ae%e5%88%b0mysql%e8%a1%a8%e4%b8%ad%ef%bc%9f%e4%bd%bf%e7%94%a8-load-data-%e7%9a%84%e4%b8%80%e7%a7%8d%e9%9d%9e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200144\/","title":{"rendered":"\u5982\u4f55\u6279\u91cf\u4e0a\u4f20CSV\u6587\u4ef6\u6570\u636e\u5230MySql\u8868\u4e2d\uff1f\u4f7f\u7528 LOAD DATA \u7684\u4e00\u79cd\u975e\u5e38\u5feb\u901f\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u6279\u91cf\u4e0a\u4f20CSV\u6587\u4ef6\u6570\u636e\u5230MySql\u8868\u4e2d\uff1f\u4f7f\u7528 LOAD DATA \u7684\u4e00\u79cd\u975e\u5e38\u5feb\u901f\u7684\u65b9\u6cd5<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<h2> ? \u7b80\u4ecb <\/h2>\n<p>\u60a8\u8fd8\u5728\u4f7f\u7528\u201cfor\u201d\u6216\u201cwhile\u201d\u5faa\u73af\u6765\u8fed\u4ee3\u884c\u5e76\u5c06\u5b83\u4eec\u63d2\u5165\u6570\u636e\u5e93\u5417\uff1f\u60a8\u8fd8\u5728\u7f16\u5199\u5355\u72ec\u7684\u4ee3\u7801\u6765\u8bfb\u53d6 .csv \u6587\u4ef6\u5e76\u5c06\u5176\u4e0a\u4f20\u5230 mysql \u6570\u636e\u5e93\u5417\uff1f<\/p>\n<p>\u7528mysql\u63d0\u4f9b\u7684\u201cload data\u201d\u8bed\u53e5\u5bf9\u7ebf\u6027\u903b\u8f91\u8bf4\u201c\u4e0d\u201d\u3002\u51c6\u5907\u597d\u66f4\u6539\u4ee3\u7801\u4ee5\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u4ee3\u7801\u884c\u5e76\u5927\u5e45\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>mysql \u63d0\u4f9b\u4e86 load data \u8bed\u53e5\uff0c\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u901f\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u5f88\u5c11\u7684\u4ee3\u7801\u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u63d2\u5165\u8868\u4e2d\u3002<\/p>\n<h2> ? \u4ec0\u4e48\u662f load data infile \u8bed\u53e5\uff1f <\/h2>\n<p>\u6839\u636e mysql \u7684\u5b98\u65b9\u6587\u6863\uff0cload data \u8bed\u53e5\u7528\u4e8e\u4ee5\u975e\u5e38\u5feb\u7684\u65b9\u5f0f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u67e5\u8be2\u5c06\u6b64\u6570\u636e\u63d2\u5165\u8868\u4e2d\uff0c\u800c\u4e0d\u662f\u591a\u6b21\u8bbf\u95ee\u6570\u636e\u5e93\u591a\u6b21\u4f7f\u7528\u201cinsert into\u201d\u67e5\u8be2\u3002<\/p>\n<p>mysql\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8bed\u53e5-&gt; &#8220;select\u2026into outfile&#8221;\uff0c\u5b83\u7684\u4f5c\u7528\u4e0e&#8221;load data&#8221;\u7684\u4f5c\u7528\u5b8c\u5168\u76f8\u53cd\uff0c\u5c31\u662f\u4ece\u8868\u4e2d\u8bfb\u53d6<strong>\u6570\u636e\u5230\u6587\u4ef6<\/strong>\u3002 <\/p>\n<p>mysql\u63d0\u4f9b\u7684mysqlimport\u5b9e\u7528\u7a0b\u5e8f\u5185\u90e8\u8c03\u7528\u670d\u52a1\u5668\u4e0a\u7684load data\u8bed\u53e5\u6765\u5bfc\u5165\u6570\u636e\u3002<\/p>\n<h2> ? \u5982\u4f55\u4f7f\u7528\u5b83\uff1f <\/h2>\n<p>above gist snippet shows the generic load data statements. all the available options are mentioned in the code above. <\/p>\n<p>lets now focus on a simple example of simply reading a .csv file and inserting it&#8217;s rows into a compatible table.<\/p>\n<p>a simple example :<\/p>\n<pre>load data infile \u2018data.csv\u2019 into table db.my_table\n<\/pre>\n<p>a birds-eye example with all essential options :<\/p>\n<p> \u4e0a\u9762\u7684\u8981\u70b9\u7247\u6bb5\u663e\u793a\u4e86\u901a\u7528\u8d1f\u8f7d\u6570\u636e\u8bed\u53e5\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u63d0\u5230\u4e86\u6240\u6709\u53ef\u7528\u7684\u9009\u9879\u3002 <\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5173\u6ce8\u4e00\u4e2a\u7b80\u200b\u200b\u5355\u7684\u793a\u4f8b\uff0c\u7b80\u5355\u5730\u8bfb\u53d6 .csv \u6587\u4ef6\u5e76\u5c06\u5176\u884c\u63d2\u5165\u5230\u517c\u5bb9\u7684\u8868\u4e2d\u3002 <\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a <\/p>\n<pre>load data infile \u2018data.csv\u2019 into table db.my_table\n<\/pre>\n<blockquote><p>\n  \u5305\u542b\u6240\u6709\u57fa\u672c\u9009\u9879\u7684\u9e1f\u77b0\u793a\u4f8b\uff1a <\/p>\n<p><\/p>\n<p>\u4e0d\u7528\u8bf4\uff0c\u60a8\u7684 csv \u6587\u4ef6\u5e94\u8be5\u6b63\u786e\u683c\u5f0f\u5316\u624d\u80fd\u4f7f\u6b64\u8bed\u53e5\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/blockquote>\n<p>\u77a7\uff0c\u4f7f\u7528\u4e0a\u9762\u7684\u8bed\u53e5\uff0c\u4f60\u7684\u8d85\u8fc7 10 \u4e07\u884c\u7684 .csv \u5c06\u5728\u51e0\u79d2\u949f\u5185\u5bfc\u5165\u5230\u4f60\u7684\u8868\u4e2d\u3002 <\/p>\n<p> <strong>\u73b0\u5728\uff0c\u4e00\u4e9b\u91cd\u8981\u7684\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\uff1a<\/strong> <\/p>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u5728mysql\u7ea7\u522b\u8fd0\u884c\uff0c\u56e0\u6b64\u6e90\u6587\u4ef6\u4ece\u5ba2\u6237\u7aef\u590d\u5236\u5230\u670d\u52a1\u5668\u4ee5\u4fbf\u5bfc\u5165\u5b83\u4eec\u3002 <\/p>\n<blockquote><p>\n  \u8fd9\u4f1a\u5e26\u6765\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u6b64\u4ee3\u7801\u65f6\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u8fde\u63a5\u7684\u5b89\u5168\u3002 <\/p>\n<p>\u5728mysql 8.0\u4e2d\uff0c\u4f7f\u7528local\u7684\u80fd\u529b\u9ed8\u8ba4\u8bbe\u7f6e\u4e3afalse\u3002\u7531\u4e8e\u60a8\u7684\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u5e94\u914d\u7f6e\u4e3a\u5141\u8bb8 local\uff0c\u56e0\u6b64\u6709\u4e9b\u4eba\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u9519\u8bef\u3002\n<\/p><\/blockquote>\n<h2> <\/h2>\n<p>\u5982\u679c\u51fa\u73b0\u6743\u9650\u9519\u8bef\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u542f\u7528 <\/p>\n<p>local_infile<br \/>:<br \/>\u6765\u8986\u76d6\u5b83 <\/p>\n<pre>SET GLOBAL local_infile = true;\n<\/pre>\n<p> <strong>\u6ce8\u610f\uff1a\u8986\u76d6\u6b64\u6807\u5fd7\u5e76\u4e0d\u662f\u5b89\u5168\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u662f\u627f\u8ba4\u63a5\u53d7\u98ce\u9669\uff0c\u60a8\u53ef\u4ee5\u53c2\u8003\u6b64\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/strong> <\/p>\n<p> ?\u200d? \u7ed3\u8bba <strong> <\/strong>mysql load data \u8bed\u53e5\u7528\u4e8e\u5728\u975e\u5e38\u77ed\u7684\u65f6\u95f4\u5185\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<p> \u60a8\u53ef\u4ee5\u5c06\u6b64\u4ee3\u7801\u4e0e\u4efb\u4f55\u540e\u7aefapi\u4e00\u8d77\u4f7f\u7528\u6765\u63d0\u4f9b\u6e90\u6587\u4ef6\u3002 <\/p>\n<p> \u6e90\u6587\u4ef6\u53ef\u4ee5\u662f\u4efb\u4f55\u6587\u672c\u6587\u4ef6\uff0c\u6211\u4eec\u5728\u672c\u4f8b\u4e2d\u4f7f\u7528.csv\u3002<strong> <\/strong><\/p>\n<p>load data local <\/p>\n<p>\u901a\u8fc7mysql\u5c06\u6e90\u6587\u4ef6\u590d\u5236\u5230\u60a8\u7684\u670d\u52a1\u5668\uff0c\u56e0\u6b64\u9700\u8981\u5728\u670d\u52a1\u5668\u7aef\u5b9e\u65bd\u5b89\u5168\u63aa\u65bd\u3002<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241006\/172821225767026d2108f1c.gif\" class=\"aligncenter\"><\/p>\n<h2>mysqlimport<\/h2>\n<p>\u5b9e\u7528\u7a0b\u5e8f\u5185\u90e8\u4f7f\u7528load data\u8bed\u53e5\u3002 <\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u8bed\u53e5\u4e2d\u6dfb\u52a0ignore 1 lines\u6765\u5ffd\u7565csv\u4e2d\u7684\u6807\u5934\u3002#??# #??##??##??# #??# ? \u53c2\u8003 #??# #??#\u5b98\u65b9\u6587\u6863#??# #??# #??# <\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u6279\u91cf\u4e0a\u4f20CSV\u6587\u4ef6\u6570\u636e\u5230MySql\u8868\u4e2d\uff1f\u4f7f\u7528 LOAD DATA \u7684\u4e00\u79cd\u975e\u5e38\u5feb\u901f\u7684\u65b9\u6cd5\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664 <\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u6279\u91cf\u4e0a\u4f20CSV\u6587\u4ef6\u6570\u636e\u5230My&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200144","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200144"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200144\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}