mysql left join 實現多條數據中某字段最大值的更新
在mysql中,對于更新student表中score字段的值,使其等于score表中對應student_id的最大值,可以使用以下方法:
查詢語句:
update student set score=(select max(score) from score where score.student_id=student.id)
解釋:
- left join: 將student表和score表以student_id列進行連接,對于student表中的每一行,找出score表中對應的最大score值。
- max(score): 獲取得分表中特定學生得分的最大值。
- where score.student_id=student.id: 確保score表的student_id與student表的student_id匹配。
- update student set score=(): 將student表的score字段更新為從( )中選出的最大值。
執行此查詢后,student表中每個學生的score字段將更新為他們在score表中獲得的最高分數。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END