简体   繁体   中英

Single update statement to update multiple rows mysql

I was working on generating customized report for an Online College Mgmt Systm and stuck here badly,

I have a master table named say report which has 10 rows i,e students with roll no 1 to 10. The attributes are roll , name and cs101 [this is the subject code and created with default value 0].

i have another table say each_subject_cs101 with fields roll , name and marks . But it may not have all the 10 students registered here, say 8 students are there (with roll 1 to 8..to make it simpler).

Now what i want is to update report set cs101 = marks from each_subject_cs101 of those students who are present in each_subject_cs101 . THIS WHOLE THING IN A SINGLE UPDATE STATEMENT.

NOTE: The roll no field is a primary key both the tables

What will be the query in MySQL ??

[PS: Actually all the above mentioned structures are more complex and created dynamically. I used aliases here to make it simpler]

UPDATE report r, each_subject_cs101 escs 
SET r.cs101 = escs.marks
WHERE r.roll = escs.roll

UPDATE report r
JOIN each_subject_cs101 escs
    ON escs.roll = r.roll
    SET r.cs101 = escs.marks;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM