简体   繁体   English

一条更新语句来更新多行mysql

[英]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]. 我有一个名为say report的主表,该表有10行,即学生的cs101卷号为1到10。属性是rollnamecs101 [这是主题代码,默认值为0创建]。

i have another table say each_subject_cs101 with fields roll , name and marks . 我有另一个表说each_subject_cs101与字段rollnamemarks 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). 但是它可能没有全部10名学生都在这里注册,比如说8名学生在那里(从1到8)。

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 . 现在,我想要的是更新reportcs101 = markseach_subject_cs101那些谁是存在于学生的each_subject_cs101 THIS WHOLE THING IN A SINGLE UPDATE STATEMENT. 这是单个更新语句中的全部内容。

NOTE: The roll no field is a primary key both the tables 注意:roll no字段是两个表的主键

What will be the query in MySQL ?? MySQL中的查询将是什么?

[PS: Actually all the above mentioned structures are more complex and created dynamically. [PS:实际上,上述所有结构都更为复杂,并且是动态创建的。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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