{"id":5136,"date":"2024-11-14T10:22:19","date_gmt":"2024-11-14T02:22:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5136\/"},"modified":"2024-11-14T10:22:19","modified_gmt":"2024-11-14T02:22:19","slug":"mysql%e5%ad%97%e6%ae%b5%e5%80%bc%e6%80%8e%e4%b9%88%e4%bf%ae%e6%94%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5136\/","title":{"rendered":"mysql\u5b57\u6bb5\u503c\u600e\u4e48\u4fee\u6539"},"content":{"rendered":"<blockquote><p>\n  \u4fee\u6539 mysql \u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 update \u8bed\u53e5\u66f4\u65b0\u6307\u5b9a\u5b57\u6bb5\u7684\u503c\uff0c\u5e76\u53ef\u4f7f\u7528 where \u5b50\u53e5\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u884c\uff1b\u4f7f\u7528 insert &#8230; on duplicate key update \u5728\u8bb0\u5f55\u4e0d\u5b58\u5728\u65f6\u63d2\u5165\uff0c\u5b58\u5728\u65f6\u66f4\u65b0\uff1b\u4f7f\u7528 replace \u66ff\u6362\u73b0\u6709\u8bb0\u5f55\uff1b\u4f7f\u7528 merge \u5408\u5e76\u8bb0\u5f55\u5e76\u66f4\u65b0\u6216\u63d2\u5165\u57fa\u4e8e\u6761\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/22\/2024052215572697052.jpg\" class=\"aligncenter\" title=\"mysql\u5b57\u6bb5\u503c\u600e\u4e48\u4fee\u6539\u63d2\u56fe\" alt=\"mysql\u5b57\u6bb5\u503c\u600e\u4e48\u4fee\u6539\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u4fee\u6539 MySQL \u5b57\u6bb5\u503c<\/h2>\n<p>MySQL \u4e2d\u4fee\u6539\u5b57\u6bb5\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<h3>UPDATE \u8bed\u53e5<\/h3>\n<p>UPDATE \u8bed\u53e5\u7528\u4e8e\u66f4\u65b0\u8868\u4e2d\u4e00\u884c\u6216\u591a\u884c\u6570\u636e\u7684\u6307\u5b9a\u5b57\u6bb5\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>UPDATE table_name\nSET column_name1 = new_value1, column_name2 = new_value2, ...\nWHERE condition;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>UPDATE users\nSET name = 'John Smith'\nWHERE id = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>WHERE \u5b50\u53e5<\/h3>\n<p>WHERE \u5b50\u53e5\u7528\u4e8e\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u884c\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u4f1a\u66f4\u65b0\u8868\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>UPDATE users\nSET name = 'John Smith'\nWHERE id = 1 OR id = 2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u591a\u4e2a\u5b57\u6bb5\u503c<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u5b57\u6bb5\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>UPDATE users\nSET name = 'John Smith', email = 'john.smith@example.com'\nWHERE id = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>NULL \u503c<\/h3>\n<p>\u8981\u5728\u5b57\u6bb5\u4e2d\u8bbe\u7f6e NULL \u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>SET column_name = NULL<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>UPDATE users\nSET email = NULL\nWHERE id = 2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<p>\u9664\u4e86 UPDATE \u8bed\u53e5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u66f4\u65b0\u5b57\u6bb5\u503c\uff1a<\/p>\n<ul>\n<li> <strong>INSERT &#8230; ON DUPLICATE KEY UPDATE\uff1a<\/strong>\u5982\u679c\u8bb0\u5f55\u4e0d\u5b58\u5728\u5219\u63d2\u5165\uff0c\u5982\u679c\u5b58\u5728\u5219\u66f4\u65b0\u3002<\/li>\n<li> <strong>REPLACE\uff1a<\/strong>\u66ff\u6362\u8868\u4e2d\u7684\u73b0\u6709\u8bb0\u5f55\u3002<\/li>\n<li> <strong>MERGE\uff1a<\/strong>\u5c06\u4e24\u4e2a\u8868\u7684\u8bb0\u5f55\u5408\u5e76\u5230\u4e00\u4e2a\u8868\u4e2d\uff0c\u5e76\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u66f4\u65b0\u6216\u63d2\u5165\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b57\u6bb5\u503c\u600e\u4e48\u4fee\u6539\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>\u4fee\u6539 mysql \u5b57\u6bb5\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 update \u8bed\u53e5\u66f4\u65b0\u6307\u5b9a\u5b57\u6bb5\u7684\u503c\uff0c\u5e76\u53ef\u4f7f\u7528 where \u5b50\u53e5\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u884c\uff1b\u4f7f\u7528 insert &#8230; on duplicate key update \u5728\u8bb0\u5f55\u4e0d\u5b58\u5728\u65f6\u63d2\u5165\uff0c\u5b58\u5728\u65f6\u66f4\u65b0\uff1b\u4f7f\u7528 replace \u66ff\u6362\u73b0\u6709\u8bb0\u5f55\uff1b\u4f7f\u7528 merge \u5408\u5e76\u8bb0\u5f55\u5e76\u66f4\u65b0\u6216\u63d2\u5165\u57fa\u4e8e\u6761\u4ef6\u3002 \u5982\u4f55\u4fee\u6539 MySQL \u5b57\u6bb5\u503c MySQL \u4e2d\u4fee\u6539\u5b57\u6bb5\u503c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a UPDATE \u8bed\u53e5 UPDATE \u8bed\u53e5\u7528\u4e8e\u66f4\u65b0\u8868\u4e2d\u4e00\u884c\u6216\u591a\u884c\u6570\u636e\u7684\u6307\u5b9a\u5b57\u6bb5\u503c\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a UPDATE table_name SET column_name1 = new_value1, column_name2 = new_value2, &#8230; WHERE condition; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a UPDATE users SET name = &#8216;John Smith&#8217; WHERE id = 1; \u767b\u5f55\u540e\u590d\u5236 WHERE \u5b50\u53e5 [&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-5136","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5136","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=5136"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5136\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}