{"id":205405,"date":"2025-05-29T11:04:24","date_gmt":"2025-05-29T03:04:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205405\/"},"modified":"2025-05-29T11:04:24","modified_gmt":"2025-05-29T03:04:24","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-left-join-%e5%b0%86-mysql-%e8%a1%a8%e4%b8%ad%e5%ad%97%e6%ae%b5%e6%9b%b4%e6%96%b0%e4%b8%ba%e6%9c%80%e5%a4%a7%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205405\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 LEFT JOIN \u5c06 MySQL \u8868\u4e2d\u5b57\u6bb5\u66f4\u65b0\u4e3a\u6700\u5927\u503c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 LEFT JOIN \u5c06 MySQL \u8868\u4e2d\u5b57\u6bb5\u66f4\u65b0\u4e3a\u6700\u5927\u503c\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 LEFT JOIN \u5c06 MySQL \u8868\u4e2d\u5b57\u6bb5\u66f4\u65b0\u4e3a\u6700\u5927\u503c\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733017187674bbe637741e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 left join \u66f4\u65b0 mysql \u8868\u4e2d\u7684\u5b57\u6bb5\u4e3a\u6700\u5927\u503c<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 left join \u6765\u66f4\u65b0\u8868\u4e2d\u7684\u5b57\u6bb5\u4e3a\u6700\u5927\u503c\u3002<\/p>\n<p>\u7ed9\u5b9a\u5982\u4e0b\u8868\u7ed3\u6784\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>\u8981\u5c06 student \u8868\u4e2d\u7684 score \u5b57\u6bb5\u66f4\u65b0\u4e3a score \u8868\u4e2d\u76f8\u540c student_id \u7684\u6700\u5927\u503c\uff0c\u53ef\u4ee5\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>\u8fd9\u4e2a\u8bed\u53e5\u4f7f\u7528 left join \u5c06 student \u8868\u4e0e score \u8868\u8fde\u63a5\uff0c\u6839\u636e student_id \u5b57\u6bb5\u8fdb\u884c\u8fde\u63a5\u3002\u5728\u5b50\u67e5\u8be2\u4e2d\uff0c\u4f7f\u7528 max() \u51fd\u6570\u83b7\u53d6 score \u8868\u4e2d\u6bcf\u4e2a student_id \u7684\u6700\u5927 score\uff0c\u7136\u540e\u5c06\u5176\u66f4\u65b0\u5230 student \u8868\u4e2d\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 LEFT JOIN \u5c06 MySQL \u8868\u4e2d\u5b57\u6bb5\u66f4\u65b0\u4e3a\u6700\u5927\u503c\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 LEFT JOIN \u5c06&#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":[4925],"tags":[],"class_list":["post-205405","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205405","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=205405"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205405\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}