{"id":5513,"date":"2024-11-14T13:17:44","date_gmt":"2024-11-14T05:17:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5513\/"},"modified":"2024-11-14T13:17:44","modified_gmt":"2024-11-14T05:17:44","slug":"mysql%e6%89%b9%e9%87%8f%e6%9b%b4%e6%96%b0%e6%80%8e%e4%b9%88%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5513\/","title":{"rendered":"mysql\u6279\u91cf\u66f4\u65b0\u600e\u4e48\u4f18\u5316"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u6279\u91cf\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u6280\u5de7\u6765\u4f18\u5316\u6027\u80fd\uff1a1. \u4f7f\u7528\u4e8b\u52a1\u6765\u9501\u5b9a\u6570\u636e\uff0c\u9632\u6b62\u5e76\u53d1\u95ee\u9898\uff1b2. \u4f7f\u7528\u6279\u91cf\u66f4\u65b0\u8bed\u53e5\u6765\u4e00\u6b21\u6027\u66f4\u65b0\u591a\u884c\uff1b3. \u4f7f\u7528 preparedstatements \u6765\u51cf\u5c11\u5f00\u9500\uff1b4. \u51cf\u5c11\u9501\u7ade\u4e89\uff0c\u4f7f\u7528\u4e50\u89c2\u9501\u673a\u5236\uff1b5. \u4f18\u5316\u7d22\u5f15\uff0c\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\uff1b6. \u5bf9\u4e8e\u5927\u578b\u66f4\u65b0\uff0c\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u66f4\u65b0\u673a\u5236\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052905482514305.jpg\" class=\"aligncenter\" title=\"mysql\u6279\u91cf\u66f4\u65b0\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" alt=\"mysql\u6279\u91cf\u66f4\u65b0\u600e\u4e48\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u6279\u91cf\u66f4\u65b0\u4f18\u5316<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u8fdb\u884c\u5927\u91cf\u6570\u636e\u66f4\u65b0\u65f6\uff0c\u9700\u8981\u8003\u8651\u4f18\u5316\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u8fd9\u91cc\u662f\u4e00\u4e9b\u4f18\u5316\u6279\u91cf\u66f4\u65b0\u7684\u6280\u5de7\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u4e8b\u52a1<\/strong><\/p>\n<p>\u4f7f\u7528\u4e8b\u52a1\u5c06\u591a\u4e2a\u66f4\u65b0\u64cd\u4f5c\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4f7f MySQL \u6267\u884c\u66f4\u65b0\u65f6\u6301\u6709\u4e00\u4e2a\u9501\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u5176\u4ed6\u64cd\u4f5c\u5728\u66f4\u65b0\u671f\u95f4\u4fee\u6539\u6570\u636e\uff0c\u4ece\u800c\u51cf\u5c11\u5e76\u53d1\u95ee\u9898\u548c\u4e22\u5931\u66f4\u65b0\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>START TRANSACTION;\nUPDATE table_name SET column_name = new_value WHERE ...;\nUPDATE table_name SET column_name = new_value WHERE ...;\nCOMMIT;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6279\u91cf\u66f4\u65b0\u8bed\u53e5<\/strong><\/p>\n<p>MySQL \u63d0\u4f9b\u4e86 UPDATE &#8230; WHERE IN (&#8230;) \u548c UPDATE &#8230; WHERE EXISTS (&#8230;) \u8bed\u53e5\uff0c\u7528\u4e8e\u6279\u91cf\u66f4\u65b0\u3002\u8fd9\u4e9b\u8bed\u53e5\u5141\u8bb8\u4e00\u6b21\u66f4\u65b0\u591a\u884c\uff0c\u800c\u65e0\u9700\u6267\u884c\u591a\u4e2a\u5355\u72ec\u7684\u66f4\u65b0\u8bed\u53e5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>UPDATE table_name SET column_name = new_value WHERE id IN (1, 2, 3);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 PreparedStatements<\/strong><\/p>\n<p>PreparedStatements \u53ef\u901a\u8fc7\u5c06 SQL \u67e5\u8be2\u548c\u6570\u636e\u5206\u5f00\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u8fd9\u5141\u8bb8 MySQL \u5728\u6267\u884c\u66f4\u65b0\u4e4b\u524d\u9884\u7f16\u8bd1\u67e5\u8be2\uff0c\u4ece\u800c\u51cf\u5c11\u4e86\u89e3\u6790\u548c\u6267\u884c\u67e5\u8be2\u7684\u5f00\u9500\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>PreparedStatement preparedStatement = connection.prepareStatement(\n    \"UPDATE table_name SET column_name = ? WHERE id = ?\"\n);\n\nfor (...) {\n    preparedStatement.setString(1, newValue);\n    preparedStatement.setInt(2, id);\n    preparedStatement.addBatch();\n}\n\npreparedStatement.executeBatch();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u51cf\u5c11\u9501\u7ade\u4e89<\/strong><\/p>\n<p>\u5728\u5e76\u53d1\u73af\u5883\u4e2d\uff0c\u591a\u4e2a\u4f1a\u8bdd\u53ef\u80fd\u540c\u65f6\u5c1d\u8bd5\u66f4\u65b0\u76f8\u540c\u7684\u6570\u636e\u3002\u4e3a\u4e86\u51cf\u5c11\u9501\u7ade\u4e89\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e50\u89c2\u9501\u673a\u5236\uff0c\u5982 WHERE col = @current_col\uff0c\u5b83\u53ea\u66f4\u65b0\u5f53\u524d\u4e0e\u6570\u636e\u5e93\u4e2d\u76f8\u540c\u503c\u7684\u884c\u3002<\/p>\n<p><strong>5. \u4f18\u5316\u7d22\u5f15<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6d89\u53ca WHERE \u5b50\u53e5\u7684\u5927\u91cf\u66f4\u65b0\uff0c\u786e\u4fdd\u8868\u4e0a\u6709\u9002\u5f53\u7684\u7d22\u5f15\u975e\u5e38\u91cd\u8981\u3002\u7d22\u5f15\u5141\u8bb8 MySQL \u5feb\u901f\u627e\u5230\u8981\u66f4\u65b0\u7684\u884c\uff0c\u4ece\u800c\u52a0\u5feb\u66f4\u65b0\u8fc7\u7a0b\u3002<\/p>\n<p><strong>6. \u4f7f\u7528\u5f02\u6b65\u66f4\u65b0<\/strong><\/p>\n<p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u66f4\u65b0\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u66f4\u65b0\u673a\u5236\uff0c\u4f8b\u5982\u4f7f\u7528 MySQL \u7684\u5e76\u884c\u590d\u5236\u6216\u5f02\u6b65\u590d\u5236\u529f\u80fd\u3002\u8fd9\u5141\u8bb8\u5728\u540e\u53f0\u6267\u884c\u66f4\u65b0\uff0c\u800c\u4e0d\u963b\u585e\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6279\u91cf\u66f4\u65b0\u600e\u4e48\u4f18\u5316\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>\u5728 mysql \u4e2d\u6279\u91cf\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u6280\u5de7\u6765\u4f18\u5316\u6027\u80fd\uff1a1. \u4f7f\u7528\u4e8b\u52a1\u6765\u9501\u5b9a\u6570\u636e\uff0c\u9632\u6b62\u5e76\u53d1\u95ee\u9898\uff1b2. \u4f7f\u7528\u6279\u91cf\u66f4\u65b0\u8bed\u53e5\u6765\u4e00\u6b21\u6027\u66f4\u65b0\u591a\u884c\uff1b3. \u4f7f\u7528 preparedstatements \u6765\u51cf\u5c11\u5f00\u9500\uff1b4. \u51cf\u5c11\u9501\u7ade\u4e89\uff0c\u4f7f\u7528\u4e50\u89c2\u9501\u673a\u5236\uff1b5. \u4f18\u5316\u7d22\u5f15\uff0c\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\uff1b6. \u5bf9\u4e8e\u5927\u578b\u66f4\u65b0\uff0c\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u66f4\u65b0\u673a\u5236\u3002 MySQL \u6279\u91cf\u66f4\u65b0\u4f18\u5316 \u5728 MySQL \u4e2d\uff0c\u8fdb\u884c\u5927\u91cf\u6570\u636e\u66f4\u65b0\u65f6\uff0c\u9700\u8981\u8003\u8651\u4f18\u5316\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u8fd9\u91cc\u662f\u4e00\u4e9b\u4f18\u5316\u6279\u91cf\u66f4\u65b0\u7684\u6280\u5de7\uff1a 1. \u4f7f\u7528\u4e8b\u52a1 \u4f7f\u7528\u4e8b\u52a1\u5c06\u591a\u4e2a\u66f4\u65b0\u64cd\u4f5c\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4f7f MySQL \u6267\u884c\u66f4\u65b0\u65f6\u6301\u6709\u4e00\u4e2a\u9501\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u5176\u4ed6\u64cd\u4f5c\u5728\u66f4\u65b0\u671f\u95f4\u4fee\u6539\u6570\u636e\uff0c\u4ece\u800c\u51cf\u5c11\u5e76\u53d1\u95ee\u9898\u548c\u4e22\u5931\u66f4\u65b0\u7684\u98ce\u9669\u3002 \u793a\u4f8b\uff1a START TRANSACTION; UPDATE table_name SET column_name = new_value WHERE &#8230;; UPDATE table_name SET column_name = new_value WHERE &#8230;; COMMIT; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6279\u91cf\u66f4\u65b0\u8bed\u53e5 MySQL \u63d0\u4f9b\u4e86 UPDATE &#8230; WHERE IN (&#8230;) \u548c UPDATE &#8230; WHERE [&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-5513","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5513","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=5513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}