{"id":204824,"date":"2025-05-29T14:30:22","date_gmt":"2025-05-29T06:30:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204824\/"},"modified":"2025-05-29T14:30:22","modified_gmt":"2025-05-29T06:30:22","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-mysql-left-join-%e6%9b%b4%e6%96%b0-student-%e8%a1%a8%e7%9a%84-score-%e5%ad%97%e6%ae%b5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204824\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 MySQL LEFT JOIN \u66f4\u65b0 Student \u8868\u7684 Score \u5b57\u6bb5\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 MySQL LEFT JOIN \u66f4\u65b0 Student \u8868\u7684 Score \u5b57\u6bb5\uff1f<\/h1>\n<p>\u76ee\u524d\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 MySQL LEFT JOIN \u66f4\u65b0 Student \u8868\u7684 Score \u5b57\u6bb5\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/173181298867395e7c92344.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528 mysql left join \u66f4\u65b0 student \u8868\u4e2d\u7684 score \u5b57\u6bb5<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 left join \u6765\u4ece\u4e24\u4e2a\u8868\u4e2d\u53d6\u591a\u4e2a\u503c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 left join \u6765\u66f4\u65b0 student \u8868\u7684 score \u5b57\u6bb5\uff0c\u4f7f\u5176\u5305\u542b score \u8868\u4e2d\u6bcf\u4e2a student_id \u7684\u6700\u5927 score\u3002<\/p>\n<p><strong>\u793a\u4f8b\u8868\u548c\u6570\u636e<\/strong><\/p>\n<p>\u6211\u4eec\u4f7f\u7528\u540d\u4e3a student \u548c score \u7684\u4e24\u4e2a\u8868\u3002student \u8868\u5305\u542b\u5b66\u751f\u4fe1\u606f\uff0c\u5305\u62ec id \u548c name \u5b57\u6bb5\u3002score \u8868\u5305\u542b\u5b66\u751f\u7684\u6210\u7ee9\u4fe1\u606f\uff0c\u5305\u62ec id\u3001student_id \u548c score \u5b57\u6bb5\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>student \u8868<\/th>\n<th> <\/th>\n<th>score \u8868<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>name<\/td>\n<td> <\/td>\n<td>id<\/td>\n<td>student_id<\/td>\n<td>score<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\u5c0f\u660e<\/td>\n<td> <\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>80<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u5c0f\u7ea2<\/td>\n<td> <\/td>\n<td>2<\/td>\n<td>2<\/td>\n<td>88<\/td>\n<\/tr>\n<tr>\n<td> <\/td>\n<td> <\/td>\n<td> <\/td>\n<td>3<\/td>\n<td>1<\/td>\n<td>78<\/td>\n<\/tr>\n<tr>\n<td> <\/td>\n<td> <\/td>\n<td> <\/td>\n<td>4<\/td>\n<td>2<\/td>\n<td>98<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u66f4\u65b0\u67e5\u8be2<\/strong><\/p>\n<p>\u8981\u5c06 student \u8868\u7684 score \u5b57\u6bb5\u66f4\u65b0\u4e3a score \u8868\u4e2d\u6700\u5927 score\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>update student set score = (\n  select max(score)\n  from score\n  where score.student_id = student.id\n)<\/pre>\n<p><strong>\u67e5\u8be2\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>left join\uff1a\u8be5\u67e5\u8be2\u4f7f\u7528 left join \u6765\u8fde\u63a5 student \u548c score \u8868\uff0c\u6839\u636e student_id \u5b57\u6bb5\u8fde\u63a5\u8fd9\u4e24\u4e2a\u8868\u3002<\/li>\n<li>max() \u51fd\u6570\uff1a\u8be5\u51fd\u6570\u7528\u4e8e\u83b7\u53d6 score \u8868\u4e2d\u6bcf\u4e2a student_id \u7684\u6700\u5927 score\u3002<\/li>\n<li>where \u5b50\u53e5\uff1a\u8be5\u5b50\u53e5\u7528\u4e8e\u8fc7\u6ee4 score \u8868\uff0c\u4ec5\u9009\u62e9\u4e0e student \u8868\u4e2d\u7684 student_id \u5339\u914d\u7684\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u67e5\u8be2\u7ed3\u679c<\/strong><\/p>\n<p>\u67e5\u8be2\u6267\u884c\u540e\uff0cstudent \u8868\u4e2d\u7684 score \u5b57\u6bb5\u5c06\u66f4\u65b0\u4e3a score \u8868\u4e2d\u6bcf\u4e2a student_id \u7684\u6700\u5927\u5206\u6570\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>student \u8868<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>name<\/td>\n<td>score<\/td>\n<\/tr>\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>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 MySQL LEFT &#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-204824","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204824","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=204824"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204824\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}