update table with data from other table if not null?

后端 未结 2 1018
北海茫月
北海茫月 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条回答
  •  再見小時候
    2021-01-21 08:59

    Use the old value if the query returns null:

    UPDATE t1 
    SET product_code = 
    ifnull(
        (SELECT `value` FROM t2 WHERE t2.variant_id = t1.variant_id AND key_id = 10), 
        product_code);
    

    FYI, in mysql using the old value doesn't count as an "update" in any way (either the number of rows affected or the logged changes)

提交回复
热议问题