update table1 set table1.clumn1 = (select table2.clumn1 from table2 where table1.id = table2.id)将表2的值更新到表1,这是更新一个字段的写法。若要一次更新多个字段,SQL应该怎么写?

解决方案 »

  1.   


    update table1 
    set (table1.clumn1,table1.clumn2,...) --有几个写几个
        = 
        (select table2.clumn1,table2.clumn1,... --这里与上面字段对应即可
         from table2 
         where table1.id = table2.id
        )
      

  2.   

    meger into table1
    using table2
       on (table1.id = table2.id)
     when matched then
    update set table1.clumn1 = table2.clumn1,
               table1.clumn2 = table2.clumn2
               ......
      

  3.   

    1楼得写法是正确的
    但是最好在后面添加update的where的控制
    防止不匹配的数据被更新为null