{"id":76670,"date":"2024-10-16T08:50:15","date_gmt":"2024-10-16T00:50:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76670\/"},"modified":"2024-10-16T08:50:16","modified_gmt":"2024-10-16T00:50:16","slug":"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%9e%e5%8f%a5insert%e5%92%8creplace%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76670\/","title":{"rendered":"MySQL\u6578\u64da\u5eab\u8a9e\u53e5INSERT\u548cREPLACE\u4ecb\u7d39"},"content":{"rendered":"<h1 id=\"mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%aa%9e%e5%8f%a5insert%e5%92%8creplace%e4%bb%8b%e7%b4%b9-vtEdBLLFHT\">MySQL\u6578\u64da\u5eab\u8a9e\u53e5INSERT\u548cREPLACE\u4ecb\u7d39<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u6578\u64da\u7684\u63d2\u5165\u548c\u66f4\u65b0\u662f\u65e5\u5e38\u64cd\u4f5c\u4e2d\u6700\u5e38\u898b\u7684\u4efb\u52d9\u4e4b\u4e00\u3002\u5728MySQL\u4e2d\uff0c<code>INSERT<\/code>\u548c<code>REPLACE<\/code>\u8a9e\u53e5\u662f\u7528\u4f86\u8655\u7406\u6578\u64da\u63d2\u5165\u7684\u5169\u500b\u91cd\u8981\u5de5\u5177\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u5169\u500b\u8a9e\u53e5\u7684\u7528\u6cd5\u53ca\u5176\u5dee\u7570\u3002<\/p>\n<h2 id=\"insert%e8%aa%9e%e5%8f%a5-vtEdBLLFHT\">INSERT\u8a9e\u53e5<\/h2>\n<p><code>INSERT<\/code>\u8a9e\u53e5\u7528\u65bc\u5c07\u65b0\u8a18\u9304\u63d2\u5165\u5230\u6578\u64da\u8868\u4e2d\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>INSERT INTO \u8868\u540d (\u6b04\u4f4d1, \u6b04\u4f4d2, ...) VALUES (\u503c1, \u503c2, ...);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba<code>users<\/code>\u7684\u8868\uff0c\u5305\u542b<code>id<\/code>\u3001<code>name<\/code>\u548c<code>email<\/code>\u4e09\u500b\u6b04\u4f4d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a9e\u53e5\u63d2\u5165\u4e00\u689d\u65b0\u8a18\u9304\uff1a<\/p>\n<pre><code>INSERT INTO users (name, email) VALUES ('\u5f35\u4e09', 'zhangsan@example.com');<\/code><\/pre>\n<h3 id=\"%e5%a4%9a%e6%a2%9d%e8%a8%98%e9%8c%84%e6%8f%92%e5%85%a5-vtEdBLLFHT\">\u591a\u689d\u8a18\u9304\u63d2\u5165<\/h3>\n<p>MySQL\u4e5f\u652f\u6301\u4e00\u6b21\u63d2\u5165\u591a\u689d\u8a18\u9304\uff0c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>INSERT INTO \u8868\u540d (\u6b04\u4f4d1, \u6b04\u4f4d2, ...) VALUES (\u503c1, \u503c2, ...), (\u503c3, \u503c4, ...);<\/code><\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>INSERT INTO users (name, email) VALUES ('\u674e\u56db', 'lisi@example.com'), ('\u738b\u4e94', 'wangwu@example.com');<\/code><\/pre>\n<h2 id=\"replace%e8%aa%9e%e5%8f%a5-vtEdBLLFHT\">REPLACE\u8a9e\u53e5<\/h2>\n<p><code>REPLACE<\/code>\u8a9e\u53e5\u7684\u529f\u80fd\u985e\u4f3c\u65bc<code>INSERT<\/code>\uff0c\u4f46\u5b83\u6703\u5728\u63d2\u5165\u65b0\u8a18\u9304\u4e4b\u524d\u6aa2\u67e5\u662f\u5426\u5b58\u5728\u76f8\u540c\u7684\u4e3b\u9375\u6216\u552f\u4e00\u7d22\u5f15\u3002\u5982\u679c\u5b58\u5728\uff0c\u5247\u6703\u522a\u9664\u820a\u8a18\u9304\u4e26\u63d2\u5165\u65b0\u8a18\u9304\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u63d2\u5165\u65b0\u8a18\u9304\u3002\u5176\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>REPLACE INTO \u8868\u540d (\u6b04\u4f4d1, \u6b04\u4f4d2, ...) VALUES (\u503c1, \u503c2, ...);<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u8981\u66f4\u65b0<code>users<\/code>\u8868\u4e2d\u7684\u4e00\u689d\u8a18\u9304\uff0c\u5982\u679c<code>id<\/code>\u70ba1\u7684\u7528\u6236\u5df2\u5b58\u5728\uff0c\u5247\u66f4\u65b0\u5176\u4fe1\u606f\uff1a<\/p>\n<pre><code>REPLACE INTO users (id, name, email) VALUES (1, '\u5f35\u4e09', 'zhangsan_updated@example.com');<\/code><\/pre>\n<h3 id=\"replace%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-vtEdBLLFHT\">REPLACE\u7684\u6ce8\u610f\u4e8b\u9805<\/h3>\n<p>\u4f7f\u7528<code>REPLACE<\/code>\u6642\u9700\u8981\u6ce8\u610f\uff0c\u820a\u8a18\u9304\u6703\u88ab\u522a\u9664\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u8207\u5916\u9375\u76f8\u95dc\u7684\u6578\u64da\u5b8c\u6574\u6027\u554f\u984c\u3002\u6b64\u5916\uff0c<code>REPLACE<\/code>\u8a9e\u53e5\u7684\u6027\u80fd\u53ef\u80fd\u4e0d\u5982<code>INSERT<\/code>\uff0c\u56e0\u70ba\u5b83\u6d89\u53ca\u5230\u522a\u9664\u548c\u63d2\u5165\u7684\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"insert%e5%92%8creplace%e7%9a%84%e6%af%94%e8%bc%83-vtEdBLLFHT\">INSERT\u548cREPLACE\u7684\u6bd4\u8f03<\/h2>\n<ul>\n<li><strong>INSERT<\/strong>\uff1a\u7528\u65bc\u63d2\u5165\u65b0\u8a18\u9304\uff0c\u82e5\u4e3b\u9375\u6216\u552f\u4e00\u7d22\u5f15\u885d\u7a81\u5247\u6703\u5831\u932f\u3002<\/li>\n<li><strong>REPLACE<\/strong>\uff1a\u7528\u65bc\u63d2\u5165\u65b0\u8a18\u9304\uff0c\u82e5\u4e3b\u9375\u6216\u552f\u4e00\u7d22\u5f15\u885d\u7a81\u5247\u6703\u522a\u9664\u820a\u8a18\u9304\u4e26\u63d2\u5165\u65b0\u8a18\u9304\u3002<\/li>\n<li>\u6027\u80fd\u4e0a\uff0c<code>INSERT<\/code>\u901a\u5e38\u66f4\u5feb\uff0c\u56e0\u70ba\u5b83\u4e0d\u6d89\u53ca\u522a\u9664\u64cd\u4f5c\u3002<\/li>\n<li>\u6578\u64da\u5b8c\u6574\u6027\u65b9\u9762\uff0c<code>INSERT<\/code>\u66f4\u5b89\u5168\uff0c\u56e0\u70ba\u5b83\u4e0d\u6703\u522a\u9664\u4efb\u4f55\u73fe\u6709\u8a18\u9304\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-vtEdBLLFHT\">\u7d50\u8ad6<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c<code>INSERT<\/code>\u548c<code>REPLACE<\/code>\u8a9e\u53e5\u5404\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u548c\u512a\u7f3a\u9ede\u3002\u958b\u767c\u8005\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u8a9e\u53e5\uff0c\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u6b63\u78ba\u6027\u548c\u5b8c\u6574\u6027\u3002\u7121\u8ad6\u662f\u9032\u884c\u6578\u64da\u63d2\u5165\u9084\u662f\u66f4\u65b0\uff0c\u4e86\u89e3\u9019\u4e9b\u57fa\u672c\u64cd\u4f5c\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u63d0\u4f9b\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u5b58\u5132\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MySQL\u6578\u64da\u5eab\u4e2d\u7684INSERT\u548cREPLACE\u8a9e\u53e5\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u5730\u63d2\u5165\u548c\u66f4\u65b0\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76670","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76670","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76670"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76670\/revisions"}],"predecessor-version":[{"id":76671,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76670\/revisions\/76671"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}