简体   繁体   English

更新所有表记录及其来自另一个表的相应数据

[英]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.

相关问题 获取具有一列的一个表的所有列,该列必须从与其数据对应的另一个表派生数据 - Get all Columns of One table having one column which has to derive data from another table corresponding to its data 更新具有另一个表的ID的表中的记录 - Updating records in table with ID from another table MySQL:使用来自另一个表的计算更新所有记录 - MySQL: Updating all records using calculations from another table 从mysql中的表中获取必须在另一个表中具有相应记录的数据 - Fetch data from table in mysql that must have corresponding records in another table 从另一个表的数据更新表的所有列? - Updating all columns of a table from another table's data? 查询以检索一个表中的主记录以及来自另一表的所有子记录 - Query to retrieve main records in one table and its all sub records from another table 使用过滤更新另一个表中的记录 - updating records from another table with filtering 如何删除表中具有另一个表中相应记录的所有记录 - How do I delete all the records in a table that have corresponding records in another table 从另一个表中选择相应的记录,但仅选择最后一个 - Select corresponding records from another table, but just the last one 从一个表中选择所有项目,然后检查另一个表中是否有相应的项目 - Select all items from a table and check if there is corresponding items in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM