{"id":200965,"date":"2025-05-06T09:07:01","date_gmt":"2025-05-06T01:07:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200965\/"},"modified":"2025-05-06T09:07:01","modified_gmt":"2025-05-06T01:07:01","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-update-%e5%92%8c-left-join-%e6%9b%b4%e6%96%b0%e5%a4%9a%e6%9d%a1%e6%95%b0%e6%8d%ae%e4%b8%ad%e7%9a%84%e6%9c%80%e5%a4%a7%e5%ad%97%e6%ae%b5%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200965\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL Update \u548c Left Join \u66f4\u65b0\u591a\u6761\u6570\u636e\u4e2d\u7684\u6700\u5927\u5b57\u6bb5\u503c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 MySQL Update \u548c Left Join \u66f4\u65b0\u591a\u6761\u6570\u636e\u4e2d\u7684\u6700\u5927\u5b57\u6bb5\u503c\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 MySQL Update \u548c Left Join \u66f4\u65b0\u591a\u6761\u6570\u636e\u4e2d\u7684\u6700\u5927\u5b57\u6bb5\u503c\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u5e93\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u4e3b\u673a\u5b9d\u8d1d\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/1732343286674175f6eb02e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql update \u8bed\u53e5\u4f7f\u7528 left join \u66f4\u65b0\u591a\u6761\u6570\u636e\u4e2d\u7684\u6700\u5927\u5b57\u6bb5\u503c<\/strong><\/p>\n<p>\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u66f4\u65b0\u8868\u4e2d\u7684\u67d0\u4e00\u5217\u4e3a\u5176\u4ed6\u8868\u4e2d\u76f8\u5173\u884c\u7684\u6700\u5927\u503c\u3002\u5bf9\u4e8e mysql \u800c\u8a00\uff0c\u53ef\u4ee5\u4f7f\u7528 left join \u6765\u5b9e\u73b0\u8fd9\u6837\u7684\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n<p>\u8003\u8651\u6211\u4eec\u6709\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1a<\/p>\n<p><strong>student \u8868<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u5c0f\u660e<\/td>\n<td>null<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u5c0f\u7ea2<\/td>\n<td>null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>score \u8868<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>student_id<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>80<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>88<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>1<\/td>\n<td>78<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>2<\/td>\n<td>98<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u7684\u76ee\u6807\u662f\u5c06 student \u8868\u4e2d\u6bcf\u4e2a\u5b66\u751f\u7684 score \u5b57\u6bb5\u66f4\u65b0\u4e3a\u5176\u5728 score \u8868\u4e2d\u5bf9\u5e94\u7684\u6700\u5927\u503c\u3002<\/p>\n<p>\u4e3a\u4e86\u4f7f\u7528 left join \u6765\u5b9e\u73b0\u6b64\u66f4\u65b0\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b sql \u8bed\u53e5\uff1a<\/p>\n<pre>update student set score=(select max(score) from score where score.student_id=student.id)<\/pre>\n<p>\u5728\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff1a<\/p>\n<ul>\n<li>update student \u90e8\u5206\u6307\u5b9a\u8981\u66f4\u65b0\u7684\u8868\u662f student \u8868\u3002<\/li>\n<li>set score=(select max(score) from score where score.student_id=student.id) \u90e8\u5206\u5b9a\u4e49\u4e86\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\u503c\u3002<\/li>\n<li>\u5b50\u67e5\u8be2 (select max(score) from score where score.student_id=student.id) \u4ece score \u8868\u4e2d\u9009\u62e9\u6bcf\u4e2a\u5b66\u751f\u7684\u6700\u5927\u5206\u503c\uff0c\u5176\u4e2d where \u5b50\u53e5\u786e\u4fdd\u53ea\u9009\u62e9\u4e0e\u5b66\u751f\u5339\u914d\u7684\u5206\u503c\u3002<\/li>\n<\/ul>\n<p>\u6267\u884c\u6b64\u8bed\u53e5\u540e\uff0cstudent \u8868\u4e2d\u6bcf\u4e2a\u5b66\u751f\u7684 score \u5b57\u6bb5\u5c06\u66f4\u65b0\u4e3a\u5176\u5728 score \u8868\u4e2d\u5bf9\u5e94\u7684\u6700\u5927\u503c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>id<\/th>\n<th>name<\/th>\n<th>score<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u5c0f\u660e<\/td>\n<td>80<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u5c0f\u7ea2<\/td>\n<td>98<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u4f7f\u7528 MySQL Update \u548c Left Join \u66f4\u65b0\u591a\u6761\u6570\u636e\u4e2d\u7684\u6700\u5927\u5b57\u6bb5\u503c\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 MySQL Updat&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200965","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200965"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200965\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}