如何使用另一个表中的值更新表

时间:2012-03-23 08:56:47

标签: mysql sql

我有两个表nol_art和#tmpIzm我想用表#tmpIzm中的值更新nol_art,如果它不为null,否则值保持不变
我写的代码,但有一些错误,所以它不起作用:

update nol_art
    set 
        CENA_IZM=if(xd.IZM IS NULL,' ',xd.IZM)
    from
        #tmpIzm xd
        join nol_art a on a.ART_ID=xd.ID_ART
    where 
        a.ART_ID=xd.ID_ART

2 个答案:

答案 0 :(得分:3)

试试这个:

update nol_art 
    set  
        CENA_IZM=coalesce(xd.IZM, CENA_IZM) 
    from 
        #tmpIzm xd 
        join nol_art a on a.ART_ID=xd.ID_ART 
    where  
        a.ART_ID=xd.ID_ART 

答案 1 :(得分:1)

我在DB2中做到了

UPDATE 
        document
    SET 
        department =    
            (SELECT departmentId
                FROM 
                department
            WHERE 
                document.base_child = document.base_child AND
                document.folder_child = document.folder_child)
    WHERE
        EXISTS 
        ( SELECT * FROM department 
                    WHERE department.base_child = department.base_child 
                     AND  department.folder_child = department.folder_child);

希望能帮助