Oracle SQL: Update a table with data from another table

后端 未结 7 2135
广开言路
广开言路 2020-11-22 02:01

Table 1:

id    name    desc
-----------------------
1     a       abc
2     b       def
3     c       adf

Table 2:

id    na         


        
7条回答
  •  北海茫月
    2020-11-22 02:23

    This is called a correlated update

    UPDATE table1 t1
       SET (name, desc) = (SELECT t2.name, t2.desc
                             FROM table2 t2
                            WHERE t1.id = t2.id)
     WHERE EXISTS (
        SELECT 1
          FROM table2 t2
         WHERE t1.id = t2.id )
    

    Assuming the join results in a key-preserved view, you could also

    UPDATE (SELECT t1.id, 
                   t1.name name1,
                   t1.desc desc1,
                   t2.name name2,
                   t2.desc desc2
              FROM table1 t1,
                   table2 t2
             WHERE t1.id = t2.id)
       SET name1 = name2,
           desc1 = desc2
    

提交回复
热议问题