一句sql语句问题
二个表
1, 货品号 数量......
idno qty1 .....
2, 物料号 数量......
goodsid qty2 .....
己知表2的物料号有些来自表1的货品号,有些不是
现在要求用表2的数据来更新表1 的数据:存在于表2 的物料号表1的货品号的数量加上表2的数量.也就是说,先在表2挑出物料号存在于表1的记录,假如是表3,然后在表1中相对的货品号数量加上表3的数量!
我记得可以用一句sql来搞定的,不知道怎么写.还是要用循环?
推荐阅读
怎么又出来个表3?
是不是如下:
update a set a.qty1 = a.qty1 + b.qty2
from 表1 a
join 表2 b on a.idno = b.goodsid
update 表1 set qty1=表2.qty2 from 表2 where 表1.idno=表2.goodsid
update 表1 set qty1=表1.qty1+表2.qty2
from 表1,表2
where 表1.idno=表2.goodsid
update 表1 set qty1=qty1+(select qty2 from 表2 a where a.goodsid=idno) where idno in (select b.idno from 表1 b,表2 c where b.idno=c.goodsid)
update 表1 set qty1=表1.qty1+表2.qty2 from 表2 where 表1.idno=表2.goodsid
update 表1 set qty1=表1.qty1+表2.qty2 from 表2 where 表1.idno=表2.goodsid
update 表1 set qty1=表2.qty2 from 表2 where 表1.idno=表2.goodsid
update 表1 set qty1=表1.qty1+表2.qty2
from 表1 inner join 表2 on 表1.idno=表2.goodsid
where 表1.idno=表2.goodsid
update 表1 set 数量=b.数量
from 表1 a inner join(select 物料号,数量=sum(数量) from 表2 group by 物料号) b on a.货品号=b.物料号
update 表1 set 数量=a.数量+b.数量
from 表1 a inner join(select 物料号,数量=sum(数量) from 表2 group by 物料号) b on a.货品号=b.物料号


讨论区