[英]Updating all table Records with with its corresponding data from another table
the thing is, i have a table students table and another scores table and i already have all the students in the scores table but the class field is still empty so, i thought i could update all of them with the data from the the students table. 事情是,我有一个学生表和另一个成绩表,我已经在成绩表中有所有的学生,但是班级字段仍然是空的,所以,我想我可以用来自学生表的数据更新所有的学生。 each students with his/her class.
每个学生上课。
UPDATE scores_tbl
INNER JOIN students_tbl
ON scores_tbl.reg_id = students_tbl.reg_id
SET scores_tbl.class = IF(students_tbl.class > 0, students_tbl.class, scores_tbl.class)
WHERE scores_tbl.session ="2018/2019";
am displayed with: Truncated incorrect DOUBLE value 显示为: 截断了错误的DOUBLE值
try this 尝试这个
UPDATE scores_tbl
INNER JOIN students_tbl
ON scores_tbl.reg_id = students_tbl.reg_id
SET scores_tbl.class = IF(IFNULL(students_tbl.class, 0) > 0, IFNULL(students_tbl.class, 0), IFNULL(scores_tbl.class, 0))
WHERE scores_tbl.session ="2018/2019";
You may try this. 您可以尝试一下。 Since you only want to update the record if
student_tbl.class
has any value. 由于您只想在
student_tbl.class
具有任何值的情况下更新记录。 It is better to write this condition in where clause and simplify your query. 最好在where子句中编写此条件并简化查询。
UPDATE scores_tbl
INNER JOIN students_tbl
ON scores_tbl.reg_id = students_tbl.reg_id
SET scores_tbl.class = students_tbl.class
WHERE scores_tbl.session ="2018/2019" and cast(students_tbl.class as int) > 0;
I am expecting that class column has only integer values. 我期望该类列只有整数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.