表A
ID,PriceA,PriceB
表B
ItemID,priceA,priceB
如何根据表B的price更新表A的相应字段。(条件是A.ID=B.ItemID)
例如:
A:1,1.3,1.5
B:1,0.2,0.3
更新后为:
A:1,1.5,1.8

解决方案 »

  1.   

    oracle:
    update A set priceA=(select priceA from B where A.id=B.Itemid);sqlserver:update A  set priceA= b1.priceA,priceB=b1.priceB
    from A a1,B b1 where a1.id=b1.Itemid;
      

  2.   

    楼上没看清这样行
    update A  set priceA= b1.priceA + a1.priceA ,priceB=b1.priceB + a1.priceB
    from A a1,B b1 where a1.id=b1.Itemid;
      

  3.   

    使用触发器
    create trigger upd_tr
    on b
    for update
    as
    begin 
      update a set a.pricea = a.pricea+ inserted.prica 
    where a.id = inserted.id
      

  4.   

    楼上的写的很清楚了这个SQL语句一点也不复杂,楼主要加强一下SQL语句的应用哦
      

  5.   

    update a set pricea=pricea+(select pricea from b where itemid=a.id),priceb=priceb+(select priceb from b where itemid=a.id)可能还有更好的方法,关注
      

  6.   

    同意  hdt(接分接出个星星)
      

  7.   

    现在项目有专门的DBA负责。我们只是做程序而已。所以以前的全忘记了。谢谢各位了。
      

  8.   

    to  qingyun1020(行出于思)update a set pricea=pricea+(select pricea from b where itemid=a.id),priceb=priceb+(select priceb from b where itemid=a.id)肯定不行,会把所有的数据都更新成一样了,呵呵~~~~
      

  9.   

    假设你的A表中的ID不重复(应该建立主键或者唯一索引),但是B表是可能重复的(只要没有约束,就要假设它可能重复,好习惯比技巧重要),那么简单地关联A、B最后只能将B中一行的数值加到A上。SQL是4GL语言,使用3GL语言的思路常常会造成误会。“update A set priceA=(select priceA from B where A.id=B.Itemid)”这种算法并不会将B中的数值累加到A上。测试一下这个简单例子:create table A(id int primary key, value int)
    insert A(id,value) values(1,1)
    create table B(id int,value int)
    insert B(id,value)
      select 1,1 union all
      select 1,2 union all
      select 1,3
    update A  set value=a.value + b.value from a,b where a.id=b.id
    select * from b
    select * from a
    go
      

  10.   

    从你的B表取名ItemID来看,这个字段很可能不是主键,仅是外键。如果果真如此,上面的所有的答案都是错误的。