有tab1表,有一条记录
a  sum
1  10.5Tab2表,有一条记录
a  sum
1  9.5怎么写update sql语句,
实现如下代码:
update tab1
set tab1.sum =tab1.sum +tab2.sum
from tab1, tab2
where tab1.a =tab2.a最后修改tab1的sum的值为20。

解决方案 »

  1.   


    --方法一:
    merge into tab1
         using tab2
            on(tab1.a =tab2.a)
      when matched then
        update set tab1.sum =tab1.sum +tab2.sum;--方法二:
    update tab1
       set tab1.sum = tab1.sum + (select tab2.sum from tab2 where tab1.a =tab2.a)
     where exists(select 1 from tab2 where tab1.a =tab2.a);--个人建议使用第一种方法。
      

  2.   

    update tab1
    set tab1.sum =(select tab1.sum +tab2.sum
    from tab1, tab2
    where tab1.a =tab2.a)
    --如果值是唯一的话:
    where exists(select 1 from from tab1, tab2
    where tab1.a =tab2.a)