update table with data from other table if not null?

后端 未结 2 999
北海茫月
北海茫月 2021-01-21 08:31

Basically what I want to do is copy the value of a column from one table to another column in another table.

The query I am using is:

UPDATE t1 
SET prod         


        
2条回答
  •  旧时难觅i
    2021-01-21 08:46

    You should just being doing the update across a join like this

    UPDATE
    t1 INNER JOIN t2 ON t1.variant_id = t2.variant_id
    SET t1.product_code = t2.value
    WHERE t2.key_id = 10
    AND t2.value IS NOT NULL
    

    There is no need to worry about nulls in that case as the inner join will only select rows where the variant_id exists in both tables.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题