{"id":5519,"date":"2024-11-14T10:56:22","date_gmt":"2024-11-14T02:56:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5519\/"},"modified":"2024-11-14T10:56:22","modified_gmt":"2024-11-14T02:56:22","slug":"mysql%e4%b9%90%e8%a7%82%e9%94%81%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5519\/","title":{"rendered":"mysql\u4e50\u89c2\u9501\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql\u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u901a\u8fc7\u4f7f\u7528\u7248\u672c\u53f7\u5217\u548cwhere\u5b50\u53e5\u786e\u4fdd\u4e8b\u52a1\u63d0\u4ea4\u65f6\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u4f18\u70b9\u5305\u62ec\u51cf\u5c11\u9501\u4e89\u7528\u548c\u63d0\u9ad8\u5e76\u53d1\u6027\uff0c\u7f3a\u70b9\u662f\u53ef\u80fd\u53d1\u751f\u5e7b\u8bfb\u548c\u9700\u8981\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u51b2\u7a81\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052907332353737.jpg\" class=\"aligncenter\" title=\"mysql\u4e50\u89c2\u9501\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u4e50\u89c2\u9501\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL\u4e50\u89c2\u9501\uff1a\u5982\u4f55\u4f7f\u7528<\/strong><\/p>\n<p>\u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u5b83\u5047\u8bbe\u4e8b\u52a1\u5728\u63d0\u4ea4\u65f6\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u4e0e\u60b2\u89c2\u9501\u76f8\u53cd\uff0c\u4e50\u89c2\u9501\u4e0d\u4f1a\u5728\u4e8b\u52a1\u5f00\u59cb\u65f6\u5c31\u9501\u5b9a\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528MySQL\u4e50\u89c2\u9501\uff1a<\/strong><\/p>\n<p><strong>1. \u8bbe\u7f6e\u7248\u672c\u53f7\u5217<\/strong><\/p>\n<p>\u4e3a\u7ecf\u5e38\u66f4\u65b0\u7684\u8868\u6dfb\u52a0\u4e00\u4e2a\u7248\u672c\u53f7\u5217\uff0c\u5982 version \u6216 updated_at\u3002\u8be5\u5217\u7528\u4e8e\u8ddf\u8e2a\u8bb0\u5f55\u7684\u7248\u672c\u3002<\/p>\n<p><strong>2. \u5728\u66f4\u65b0\u8bed\u53e5\u4e2d\u4f7f\u7528WHERE\u5b50\u53e5<\/strong><\/p>\n<p>\u5728\u66f4\u65b0\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u7248\u672c\u53f7\u5217\u7684WHERE\u5b50\u53e5\u6765\u786e\u4fdd\u53ea\u6709\u4e0e\u9884\u671f\u7248\u672c\u5339\u914d\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>UPDATE table_name\nSET name = 'New Name'\nWHERE version = 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5904\u7406\u51b2\u7a81<\/strong><\/p>\n<p>\u5982\u679c\u5728\u4e8b\u52a1\u63d0\u4ea4\u65f6\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u5df2\u66f4\u6539\uff0c\u5219\u66f4\u65b0\u64cd\u4f5c\u5c06\u5931\u8d25\u3002MySQL\u5c06\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\uff0c\u6307\u793a\u4e50\u89c2\u9501\u51b2\u7a81\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u51cf\u5c11\u9501\u4e89\u7528\uff0c\u63d0\u9ad8\u5e76\u53d1\u6027<\/li>\n<li>\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u4e8b\u52a1\uff0c\u907f\u514d\u6b7b\u9501<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u53ef\u80fd\u53d1\u751f\u5e7b\u8bfb\uff0c\u5373\u66f4\u65b0\u4e86\u4e00\u6761\u8bb0\u5f55\uff0c\u5374\u770b\u4e0d\u5230\u5176\u4ed6\u4e8b\u52a1\u5bf9\u5176\u7684\u66f4\u65b0<\/li>\n<li>\u9700\u8981\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u4e50\u89c2\u9501\u51b2\u7a81<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u540d\u4e3ausers\u7684\u8868\uff0c\u5176\u4e2d\u6709name\u548cversion\u5217\u3002\u8981\u66f4\u65b0id\u4e3a1\u7684\u7528\u6237\u7684\u59d3\u540d\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<pre>BEGIN TRANSACTION;\nSELECT version INTO @old_version FROM users WHERE id = 1 FOR UPDATE;\nUPDATE users SET name = 'New Name' WHERE id = 1 AND version = @old_version;\nCOMMIT;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u5728\u7b2c\u4e00\u4e2a\u4e8b\u52a1\u5f00\u59cb\u540e\u66f4\u65b0\u4e86\u8be5\u8bb0\u5f55\uff0c\u7b2c\u4e8c\u4e2a\u4e8b\u52a1\u5c06\u5931\u8d25\uff0c\u5e76\u629b\u51fa\u4e50\u89c2\u9501\u51b2\u7a81\u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e50\u89c2\u9501\u600e\u4e48\u7528\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>mysql\u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u901a\u8fc7\u4f7f\u7528\u7248\u672c\u53f7\u5217\u548cwhere\u5b50\u53e5\u786e\u4fdd\u4e8b\u52a1\u63d0\u4ea4\u65f6\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u4f18\u70b9\u5305\u62ec\u51cf\u5c11\u9501\u4e89\u7528\u548c\u63d0\u9ad8\u5e76\u53d1\u6027\uff0c\u7f3a\u70b9\u662f\u53ef\u80fd\u53d1\u751f\u5e7b\u8bfb\u548c\u9700\u8981\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u51b2\u7a81\u3002 MySQL\u4e50\u89c2\u9501\uff1a\u5982\u4f55\u4f7f\u7528 \u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u5b83\u5047\u8bbe\u4e8b\u52a1\u5728\u63d0\u4ea4\u65f6\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u4e0e\u60b2\u89c2\u9501\u76f8\u53cd\uff0c\u4e50\u89c2\u9501\u4e0d\u4f1a\u5728\u4e8b\u52a1\u5f00\u59cb\u65f6\u5c31\u9501\u5b9a\u6570\u636e\u3002 \u5982\u4f55\u4f7f\u7528MySQL\u4e50\u89c2\u9501\uff1a 1. \u8bbe\u7f6e\u7248\u672c\u53f7\u5217 \u4e3a\u7ecf\u5e38\u66f4\u65b0\u7684\u8868\u6dfb\u52a0\u4e00\u4e2a\u7248\u672c\u53f7\u5217\uff0c\u5982 version \u6216 updated_at\u3002\u8be5\u5217\u7528\u4e8e\u8ddf\u8e2a\u8bb0\u5f55\u7684\u7248\u672c\u3002 2. \u5728\u66f4\u65b0\u8bed\u53e5\u4e2d\u4f7f\u7528WHERE\u5b50\u53e5 \u5728\u66f4\u65b0\u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528\u7248\u672c\u53f7\u5217\u7684WHERE\u5b50\u53e5\u6765\u786e\u4fdd\u53ea\u6709\u4e0e\u9884\u671f\u7248\u672c\u5339\u914d\u7684\u8bb0\u5f55\u88ab\u66f4\u65b0\u3002\u4f8b\u5982\uff1a UPDATE table_name SET name = &#8216;New Name&#8217; WHERE version = 1 \u767b\u5f55\u540e\u590d\u5236 3. \u5904\u7406\u51b2\u7a81 \u5982\u679c\u5728\u4e8b\u52a1\u63d0\u4ea4\u65f6\u8bb0\u5f55\u7684\u7248\u672c\u53f7\u5df2\u66f4\u6539\uff0c\u5219\u66f4\u65b0\u64cd\u4f5c\u5c06\u5931\u8d25\u3002MySQL\u5c06\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\uff0c\u6307\u793a\u4e50\u89c2\u9501\u51b2\u7a81\u3002 \u4f18\u70b9\uff1a \u51cf\u5c11\u9501\u4e89\u7528\uff0c\u63d0\u9ad8\u5e76\u53d1\u6027 \u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u4e8b\u52a1\uff0c\u907f\u514d\u6b7b\u9501 \u7f3a\u70b9\uff1a \u53ef\u80fd\u53d1\u751f\u5e7b\u8bfb\uff0c\u5373\u66f4\u65b0\u4e86\u4e00\u6761\u8bb0\u5f55\uff0c\u5374\u770b\u4e0d\u5230\u5176\u4ed6\u4e8b\u52a1\u5bf9\u5176\u7684\u66f4\u65b0 \u9700\u8981\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u4e50\u89c2\u9501\u51b2\u7a81 \u793a\u4f8b\uff1a \u8003\u8651\u4e00\u4e2a\u540d\u4e3ausers\u7684\u8868\uff0c\u5176\u4e2d\u6709name\u548cversion\u5217\u3002\u8981\u66f4\u65b0id\u4e3a1\u7684\u7528\u6237\u7684\u59d3\u540d\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a BEGIN TRANSACTION; SELECT version INTO @old_version FROM users WHERE id = 1 FOR UPDATE; UPDATE users SET name = [&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-5519","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5519","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=5519"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5519\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}