如何使用 left join 更新 student 表中 score 字段為對應 score 表中最大值?

如何使用 left join 更新 student 表中 score 字段為對應 score 表中最大值?

mysql使用left join更新表中多條數據中的最大值

問題:

如何將student表中score字段更新為對應score表中最大值的score?

數據結構

  • student表:id、name、score
  • score表:id、student_id、score

sql語句

update student set score=(select max(score) from score where score.student_id=student.id)

說明:

sql語句使用left join連接student表和score表,根據student.id和score.student_id字段進行匹配。對于每個student表中的記錄,它會從score表中獲取關聯的score值的列表,然后使用max()函數計算列表中的最大值。最后,將最大值更新到student表的score字段。

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享