【问题标题】:Updating all table Records with with its corresponding data from another table使用来自另一个表的相应数据更新所有表记录
【发布时间】:2019-08-22 07:34:32
【问题描述】:

问题是,我有一个学生表和另一个分数表,我已经在分数表中有所有学生,但班级字段仍然是空的,所以我想我可以用来自学生表。每个学生和他/她的班级。

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";

显示:截断不正确的 DOUBLE 值

【问题讨论】:

  • 表定义和示例数据会很好。
  • "Truncated incorrect DOUBLE value" 表示您正在尝试比较 WHERE 或 ON 子句中的数字和字符串。 students_tbl.classscores_tbl.reg_idstudents_tbl.reg_id的数据类型是什么?
  • @JoanLaraGanau 数据类型是 varchar

标签: mysql sql phpmyadmin


【解决方案1】:

试试这个

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";

【讨论】:

    【解决方案2】:

    你可以试试这个。因为您只想在 student_tbl.class 有任何值时更新记录。最好将此条件写在 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;
    

    我希望类列只有整数值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      相关资源
      最近更新 更多