{"id":32673,"date":"2023-05-31T11:08:38","date_gmt":"2023-05-31T03:08:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=32673"},"modified":"2023-07-31T00:19:46","modified_gmt":"2023-07-30T16:19:46","slug":"mysql%e4%b8%ad%ef%bc%8creplace-into%e5%90%8e%e6%80%8e%e4%b9%88%e8%bf%9e%e6%8e%a5where%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/32673.html","title":{"rendered":"mysql\u4e2d\uff0creplace into\u540e\u600e\u4e48\u8fde\u63a5where\u5b50\u53e5"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>REPLACE INTO \u8bed\u6cd5\u5b8c\u5168 \u4e0e INSERT INTO \u4e00\u6837\u5de5\u4f5c\uff0c\u53ea\u662f\u5728\u65b0\u884c\u5df2\u63d2\u5165\u3002\u8fd9\u610f\u5473\u7740\u60a8\u5e94\u8be5\u5c06\u4e3b\u952e\u6dfb\u52a0\u5230\u88ab\u66ff\u6362\u7684\u503c\u4e2d\uff0c\u800c\u4e0d\u662f WHERE \u5b50\u53e5\u4ee5\u9650\u5236\u66f4\u65b0\u3002replace into\u8bed\u53e5\u4f1a\u6839\u636e\u5c11\u6570\u952e\/\u4e3b\u952e\u5220\u9664\u539f\u6709\u7684\u4e00\u6761\u8bb0\u5f55\uff0c \u5e76\u4e14\u63d2\u5165\u4e00\u6761\u65b0\u7684\u8bb0\u5f55\u6765\u66ff\u6362\u539f\u8bb0\u5f55\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-32679\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-16.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-16.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/05\/\u6dfb\u52a0\u6807\u9898-15-16-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001mysql\u4e2d\uff0creplace into\u540e\u600e\u4e48\u8fde\u63a5where\u5b50\u53e5<\/strong><\/h2>\n<p>REPLACE INTO\u00a0\u8bed\u6cd5<em>\u5b8c\u5168<\/em>\u00a0\u4e0e\u00a0INSERT INTO\u00a0\u4e00\u6837\u5de5\u4f5c\uff0c\u53ea\u662f\u5728\u65b0\u884c\u5df2\u63d2\u5165\u3002<\/p>\n<p>\u8fd9\u610f\u5473\u7740\u60a8\u5e94\u8be5\u5c06\u4e3b\u952e\u6dfb\u52a0\u5230\u88ab\u66ff\u6362\u7684\u503c\u4e2d\uff0c\u800c\u4e0d\u662f\u00a0WHERE\u00a0\u5b50\u53e5\u4ee5\u9650\u5236\u66f4\u65b0\u3002<\/p>\n<p>REPLACE INTO myTable (<\/p>\n<p>\u00a0 myPrimaryKey,<\/p>\n<p>\u00a0 myColumn1,<\/p>\n<p>\u00a0 myColumn2<\/p>\n<p>) VALUES (<\/p>\n<p>\u00a0 100,<\/p>\n<p>\u00a0 \u2018value1\u2019,<\/p>\n<p>\u00a0 \u2018value2\u2019<\/p>\n<p>);<\/p>\n<p>\u2026\u5c06\u63d0\u4f9b\u4e0e\u2026\u76f8\u540c\u7684\u7ed3\u679c<\/p>\n<p>UPDATE myTable<\/p>\n<p>SET myColumn1 = \u2018value1\u2019, myColumn2 = \u2018value2\u2019<\/p>\n<p>WHERE myPrimaryKey = 100;<\/p>\n<p>\u2026\u6216\u66f4\u51c6\u786e\u5730\u8bf4:<\/p>\n<p>DELETE FROM myTable WHERE myPrimaryKey = 100;<\/p>\n<p>INSERT INTO myTable(<\/p>\n<p>\u00a0 myPrimaryKey,<\/p>\n<p>\u00a0 myColumn1,<\/p>\n<p>\u00a0 myColumn2<\/p>\n<p>) VALUES (<\/p>\n<p>\u00a0 100,<\/p>\n<p>\u00a0 \u2018value1\u2019,<\/p>\n<p>\u00a0 \u2018value2\u2019<\/p>\n<p>);<\/p>\n<p>replace into\u8bed\u53e5\u4f1a\u6839\u636e\u5c11\u6570\u952e\/\u4e3b\u952e\u5220\u9664\u539f\u6709\u7684\u4e00\u6761\u8bb0\u5f55\uff0c \u5e76\u4e14\u63d2\u5165\u4e00\u6761\u65b0\u7684\u8bb0\u5f55\u6765\u66ff\u6362\u539f\u8bb0\u5f55\u3002\u5982\u679c\u6ca1\u6709\u8868\u4e0d\u5b58\u5728\u5c11\u6570\u952e\/\u4e3b\u952e \u6216\u8005 \u63d2\u5165\u7684\u6570\u636e\u5c11\u6570\u952e\/\u4e3b\u952e\u6ca1\u6709\u91cd\u590d\u7684\uff0c\u90a3\u4e48\u4f1a\u65b0\u589e\u4e00\u6761\u8bb0\u5f55\u3002<\/p>\n<p>\u65b0\u5efa\u4e00\u4e2atest\u8868\uff0c\u4e09\u4e2a\u5b57\u6bb5\uff0cuid\uff0cname\uff0cmobile, id\u662f\u81ea\u589e\u7684\u4e3b\u952e\uff0cmobile\u662f\u5c11\u6570\u7d22\u5f15\uff1b<\/p>\n<p>create table test(<\/p>\n<p>\u00a0\u00a0\u00a0 uid int(11) NOT NULL AUTO_INCREMENT,<\/p>\n<p>\u00a0\u00a0\u00a0 name varchar(50),<\/p>\n<p>\u00a0\u00a0\u00a0 mobile varchar(11) unique,<\/p>\n<p>\u00a0\u00a0\u00a0 PRIMARY KEY (uid)<\/p>\n<p>);<\/p>\n<p>#\u63d2\u5165\u4e24\u6761\u6570\u636e\u8fdb\u884c\u6f14\u793a<\/p>\n<p>insert into\u00a0 test(name,mobile) VALUES (\u2018\u5f20\u4e09\u2019,\u2019135\u2032);<\/p>\n<p>insert into\u00a0 test(name,mobile) VALUES (\u2018\u674e\u56db\u2019,\u2019137\u2032);<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u8981\u6301\u4e45\u5316\u8fde\u63a5<\/strong><\/h2>\n<p>\u8fd9\u548c web \u670d\u52a1\u5668\u7684\u5de5\u4f5c\u65b9\u5f0f\u6709\u5173\u3002web \u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u4e09\u79cd\u65b9\u6cd5\u6765\u5229\u7528 PHP \u751f\u6210 web \u9875\u9762\u3002<\/p>\n<p>\u540d\u5217\u524d\u8305\u79cd\u65b9\u6cd5\u662f\u5c06 PHP \u81ea\u4ee5\u4e3a CGI\u201c\u5305\u88c5\u5668\u201d\u7528\u4f5c\u4e00\u4e2a\u5355\u72ec\u8fd0\u884c\u7684\u8bed\u8a00\u89e3\u91ca\u5668(CGI Wapper)\u3002\u5f53\u4ee5\u8fd9\u79cd\u65b9\u6cd5\u8fd0\u884c\u65f6\uff0cPHP \u4f1a\u4e3a\u5411 web \u670d\u52a1\u5668\u7684\u6bcf\u4e2a PHP \u9875\u9762\u8bf7\u6c42\u521b\u5efa\u5e76\u9500\u6bc1 PHP \u89e3\u91ca\u5668\u7684\u65f6\u5019\u5b9e\u4f8b\u3002\u7531\u4e8e\u5176\u4f1a\u968f\u6bcf\u4e2a\u8bf7\u6c42\u7684\u7ed3\u675f\u800c\u9500\u6bc1\uff0c\u56e0\u6b64\u5176\u83b7\u53d6\u7684\u4efb\u4f55\u8d44\u6e90\uff08\u4f8b\u5982\u6307\u5411 SQL \u6570\u636e\u5e93\u670d\u52a1\u5668\u7684\u94fe\u63a5\uff09\u90fd\u4f1a\u5728\u9500\u6bc1\u65f6\u5173\u95ed\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u4ece\u4f7f\u7528\u6301\u4e45\u8fde\u63a5\u4e2d\u83b7\u5f97\u4efb\u4f55\u597d\u5904\u2014\u2014\u56e0\u4e3a\u6839\u672c\u4e0d\u4f1a\u6301\u4e45\u3002<\/p>\n<p>\u7b2c\u4e8c\uff0c\u4e5f\u662f\u6700\u6d41\u884c\u7684\u65b9\u6cd5\u662f\u628a PHP \u7528\u4f5c\u591a\u8fdb\u7a0b web \u670d\u52a1\u5668\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u8fd9\u79cd\u65b9\u6cd5\u76ee\u524d\u53ea\u9002\u7528\u4e8e Apache\u3002\u591a\u8fdb\u7a0b\u7684\u670d\u52a1\u5668\u901a\u5e38\u6709\u4e00\u4e2a\u7236\u8fdb\u7a0b\u548c\u4e00\u7ec4\u5b50\u8fdb\u7a0b\u534f\u8c03\u8fd0\u884c\uff0c\u5b50\u8fdb\u7a0b\u8d1f\u8d23\u63d0\u4f9b\u7f51\u9875\u7684\u5de5\u4f5c\u3002\u6bcf\u5f53\u63a5\u6536\u8fbe\u5230\u5ba2\u6237\u7aef\u63d0\u51fa\u8bf7\u6c42\u65f6\uff0c\u8be5\u8bf7\u6c42\u4f1a\u4f20\u9012\u7ed9\u5c1a\u672a\u7ed9\u5176\u5b83\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\u7684\u67d0\u4e2a\u5b50\u8fdb\u7a0b\u3002\u8fd9\u4e5f\u5c31\u662f\u8bf4\u5f53\u76f8\u540c\u7684\u5ba2\u6237\u7aef\u7b2c\u4e8c\u6b21\u5411\u670d\u52a1\u7aef\u53d1\u51fa\u8bf7\u6c42\u65f6\uff0c\u5b83\u5c06\u6709\u53ef\u80fd\u7531\u4e0e\u540d\u5217\u524d\u8305\u6b21\u4e0d\u540c\u7684\u67d0\u4e2a\u5b50\u8fdb\u7a0b\u63d0\u4f9b\u670d\u52a1\u3002\u5728\u5f00\u542f\u4e86\u4e00\u4e2a\u6301\u4e45\u8fde\u63a5\u540e\uff0c\u6240\u6709\u8bf7\u6c42 SQL \u670d\u52a1\u7684\u540e\u7ee7\u9875\u9762\u90fd\u80fd\u591f\u91cd\u7528\u4e0e SQL \u670d\u52a1\u5668\u5efa\u7acb\u7684\u76f8\u540c\u8fde\u63a5\u3002<\/p>\n<p>\u6700\u540e\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06 PHP \u7528\u4f5c\u591a\u7ebf\u7a0b web \u670d\u52a1\u5668\u7684\u63d2\u4ef6\u3002\u76ee\u524d PHP \u652f\u6301 WSAPI \u548c NSAPI\uff08\u5728 Windows \u4e0a\uff09\uff0c\u5141\u8bb8 PHP \u4f5c\u4e3a Netscape FastTrack\uff08iPlanet\uff09\u3001Microsoft \u7684 Internet Information Server (IIS) \u548c O\u2019Reilly \u7684 WebSite Pro \u7b49\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u63d2\u4ef6\u4f7f\u7528\u3002\u8be5\u884c\u4e3a\u4e0e\u524d\u9762\u63cf\u8ff0\u7684\u591a\u8fc7\u7a0b\u6a21\u578b\u76f8\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"REPLACE INTO \u8bed\u6cd5\u5b8c\u5168 \u4e0e INSERT INTO \u4e00\u6837\u5de5\u4f5c\uff0c\u53ea\u662f\u5728\u65b0\u884c\u5df2\u63d2\u5165\u3002\u8fd9\u610f\u5473\u7740\u60a8\u5e94\u8be5\u5c06\u4e3b [&hellip;]","protected":false},"author":1,"featured_media":32679,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32673"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=32673"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/32673\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/32679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=32673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=32673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=32673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}