mysql left join 更新语句筛选最大值字段
问题:
如何使用 mysql 中的 left join 关联两个表,并根据第二个表中的字段更新第一个表中相应字段的最大值?
示例:
假设有两个表:student 和 score。student 表中包含学生信息(id 和 name),score 表包含学生成绩记录(student_id 和 score)。目标是将 student 表的 score 字段更新为关联的 score 表中分数最高的那个值。
解决方案:
可以使用以下 update 语句实现此目的:
update student set score=(select max(score) from score where score.student_id=student.id)
解析:
执行结果:
执行该 update 语句后,student 表中的 score 字段将更新为每个学生关联的 score 表中最高的分数。