{"id":2018,"date":"2020-07-29T23:14:35","date_gmt":"2020-07-29T15:14:35","guid":{"rendered":"http:\/\/39.97.231.10\/?p=2018"},"modified":"2020-07-29T23:14:35","modified_gmt":"2020-07-29T15:14:35","slug":"mysql%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%aeinsert-into-on-duplicate-key-update%ef%bc%8c%e9%87%8d%e5%a4%8d%e6%95%b0%e6%8d%ae%e8%87%aa%e5%8a%a8%e6%9b%b4%e6%96%b0","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=2018","title":{"rendered":"MySQL\u63d2\u5165\u6570\u636einsert into on duplicate key update\uff0c\u91cd\u590d\u6570\u636e\u81ea\u52a8\u66f4\u65b0"},"content":{"rendered":"<div>\n<div>\n<div>\nMySQL\u4e2d\u63d2\u5165\u6570\u636e\uff0c\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u5728\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\uff08\u4e3b\u952e\u6216\u8005\u552f\u4e00\u952e\u5df2\u5b58\u5728\uff09\uff0c\u4f7f\u7528insert into on duplicate key update \u8bed\u6cd5\u53ef\u4ee5\u66f4\u65b0\u91cd\u590d\u6570\u636e\u7684\u67d0\u4e9b\u5b57\u6bb5\u503c\u3002<\/p>\n<h4>1\u3001\u8bed\u6cd5<\/h4>\n<p>insert into table_name(field1,field2,&#8230;fieldN) values(value1,value2,&#8230;valueN) on duplicate key update fieldM = valueM;<br \/>\n\u9650\u5236\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u4e3b\u952e\u91cd\u590d<\/li>\n<li>\u552f\u4e00\u952e\u91cd\u590d<\/li>\n<\/ul>\n<h4>2\u3001\u6848\u4f8b<\/h4>\n<p>\u8868\u7ed3\u6784\u53ca\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<pre> CREATE TABLE table_name(\n\u00a0 id int(11) NOT NULL,\n\u00a0 name varchar(50) DEFAULT NULL,\n\u00a0 age int(11) DEFAULT NULL,\n\u00a0 PRIMARY KEY (id),\n\u00a0 UNIQUE KEY uk_name (name)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n<\/pre>\n<pre>mysql&gt; select * from table_name;\n+----+------+------+\n| id | name | age\u00a0 |\n+----+------+------+\n|\u00a0 1 | Tom\u00a0 |\u00a0\u00a0 20 |\n+----+------+------+\n<\/pre>\n<p>\u4ee5\u4e0b\u4e09\u6761SQL\uff0c\u6267\u884c\u7ed3\u679c\u90fd\u4e00\u6837\uff0c\u53ea\u8981\u4e3b\u952e\u6216\u8005\u552f\u4e00\u952e\u53d1\u751f\u91cd\u590d\uff0c\u5c31\u4f1a\u66f4\u65b0\u8be5\u6761\u8bb0\u5f55\u7684age\u5b57\u6bb5\u503c\uff1a<\/p>\n<ul>\n<li>insert into table_name values(1,\\&#8217;Bill\\&#8217;,21) on duplicate key update age = 21;<\/li>\n<li>insert into table_name values(2,\\&#8217;Tom\\&#8217;,21) on duplicate key update age = 21;<\/li>\n<li>insert into table_name values(1,\\&#8217;Tom\\&#8217;,21) on duplicate key update age = 21;<\/li>\n<\/ul>\n<pre>mysql&gt; select * from table_name;\n+----+------+------+\n| id | name | age\u00a0 |\n+----+------+------+\n|\u00a0 1 | Tom\u00a0 |\u00a0\u00a0 21 |\n+----+------+------+\n1 row in set (0.00 sec)\n<\/pre>\n<p>\u4ee5\u4e0a\u53ef\u4ee5\u770b\u5230age\u5b57\u6bb5\u5df2\u7ecf\u4ece20\u88ab\u66f4\u65b0\u4e3a21\u3002\n<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u4e2d\u63d2\u5165\u6570\u636e\uff0c\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u5728\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\uff08\u4e3b\u952e\u6216\u8005\u552f\u4e00\u952e\u5df2\u5b58\u5728\uff09\uff0c\u4f7f\u7528insert into on  &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=2018\"> <span class=\"screen-reader-text\">MySQL\u63d2\u5165\u6570\u636einsert into on duplicate key update\uff0c\u91cd\u590d\u6570\u636e\u81ea\u52a8\u66f4\u65b0<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":1,"uagb_excerpt":"MySQL\u4e2d\u63d2\u5165\u6570\u636e\uff0c\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u5728\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728\uff08\u4e3b\u952e\u6216\u8005\u552f\u4e00\u952e\u5df2\u5b58\u5728\uff09\uff0c\u4f7f\u7528insert into on &hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2018"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2018"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2018\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2018"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}