{"id":6277,"date":"2024-11-14T12:47:16","date_gmt":"2024-11-14T04:47:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6277\/"},"modified":"2024-11-14T12:47:16","modified_gmt":"2024-11-14T04:47:16","slug":"mysql%e6%a0%b9%e6%8d%ae%e4%b8%80%e4%b8%aa%e8%a1%a8%e7%9a%84%e6%95%b0%e6%8d%ae%e6%9b%b4%e6%96%b0%e5%8f%a6%e4%b8%80%e4%b8%aa%e8%a1%a8%e6%95%b0%e6%8d%ae%e7%9a%84sql%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6277\/","title":{"rendered":"mysql\u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684SQL\u5199\u6cd5"},"content":{"rendered":"<blockquote><p>\n  sql \u4e2d\u66f4\u65b0\u8868\u6570\u636e\u7684\u901a\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 join \u8bed\u53e5\uff0c\u5339\u914d\u4e24\u4e2a\u8868\u5e76\u66f4\u65b0\u76ee\u6807\u8868\u7684\u6307\u5b9a\u5217\uff1b\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u83b7\u53d6\u6e90\u8868\u4e2d\u5339\u914d\u884c\u7684\u503c\u5e76\u66f4\u65b0\u76ee\u6807\u8868\uff1b\u4f7f\u7528 merge \u8bed\u53e5\uff08mysql 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\uff0c\u5408\u5e76\u4e24\u4e2a\u8868\u5e76\u6309\u6761\u4ef6\u66f4\u65b0\u6216\u63d2\u5165\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/25\/2024062504351749946.jpg\" class=\"aligncenter\" title=\"mysql\u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684SQL\u5199\u6cd5\u63d2\u56fe\" alt=\"mysql\u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684SQL\u5199\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684 SQL \u5199\u6cd5<\/strong><\/p>\n<p><span>\u6700\u8fd1\u5927\u5bb6\u90fd\u5728\u770b<\/span><\/p>\n<\/p>\n<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 JOIN \u8bed\u53e5<\/strong><\/p>\n<pre>UPDATE table2 SET column2 = table1.column1\nJOIN table1 ON table2.id = table1.id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2<\/strong><\/p>\n<pre>UPDATE table2\nSET column2 = (\n    SELECT column1\n    FROM table1\n    WHERE table2.id = table1.id\n);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528 MERGE \u8bed\u53e5\uff08MySQL 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09<\/strong><\/p>\n<pre>MERGE INTO table2 AS t2\nUSING table1 AS t1\nON t2.id = t1.id\nWHEN MATCHED THEN\n  UPDATE SET column2 = t1.column1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<ul>\n<li>table1\uff1a\u5305\u542b\u5177\u6709 id \u548c name \u5217\u7684\u6570\u636e<\/li>\n<li>table2\uff1a\u5305\u542b\u5177\u6709 id \u548c description \u5217\u7684\u6570\u636e<\/li>\n<\/ul>\n<p>\u8981\u4f7f\u7528 table1 \u4e2d\u7684 name \u66f4\u65b0 table2 \u4e2d\u7684 description\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b SQL \u67e5\u8be2\uff1a<\/p>\n<pre>-- \u4f7f\u7528 JOIN \u8bed\u53e5\nUPDATE table2 SET description = table1.name\nJOIN table1 ON table2.id = table1.id;\n\n-- \u4f7f\u7528\u5b50\u67e5\u8be2\nUPDATE table2\nSET description = (\n    SELECT name\n    FROM table1\n    WHERE table2.id = table1.id\n);\n\n-- \u4f7f\u7528 MERGE \u8bed\u53e5\uff08MySQL 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\nMERGE INTO table2 AS t2\nUSING table1 AS t1\nON t2.id = t1.id\nWHEN MATCHED THEN\n  UPDATE SET description = t1.name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684SQL\u5199\u6cd5\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>sql \u4e2d\u66f4\u65b0\u8868\u6570\u636e\u7684\u901a\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 join \u8bed\u53e5\uff0c\u5339\u914d\u4e24\u4e2a\u8868\u5e76\u66f4\u65b0\u76ee\u6807\u8868\u7684\u6307\u5b9a\u5217\uff1b\u4f7f\u7528\u5b50\u67e5\u8be2\uff0c\u83b7\u53d6\u6e90\u8868\u4e2d\u5339\u914d\u884c\u7684\u503c\u5e76\u66f4\u65b0\u76ee\u6807\u8868\uff1b\u4f7f\u7528 merge \u8bed\u53e5\uff08mysql 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\uff0c\u5408\u5e76\u4e24\u4e2a\u8868\u5e76\u6309\u6761\u4ef6\u66f4\u65b0\u6216\u63d2\u5165\u6570\u636e\u3002 \u6839\u636e\u4e00\u4e2a\u8868\u7684\u6570\u636e\u66f4\u65b0\u53e6\u4e00\u4e2a\u8868\u6570\u636e\u7684 SQL \u5199\u6cd5 \u6700\u8fd1\u5927\u5bb6\u90fd\u5728\u770b \u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 JOIN \u8bed\u53e5 UPDATE table2 SET column2 = table1.column1 JOIN table1 ON table2.id = table1.id; \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u5b50\u67e5\u8be2 UPDATE table2 SET column2 = ( SELECT column1 FROM table1 WHERE table2.id = table1.id ); \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528 MERGE \u8bed\u53e5\uff08MySQL 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff09 MERGE INTO table2 AS t2 [&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-6277","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6277","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=6277"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6277\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}