日期:2014-05-18  浏览次数:20410 次

两个表连接更新
我有A表B表
A表字段 spid spbh  
  sp001 1001
  sp002 1002
  sp003 1003

B表字段 spid spbh spbh_new
  sp001 1001 1011
  sp003 1003 1013

当b.spid=a.spid 时,更新A表,得出结果
A表 spid spbh
  sp001 1011
  sp002 1002
  sp003 1013

这个语句怎么写?

------解决方案--------------------
SQL code
update a
set a.spbh=b.spbh_new
from b
where b.spid=a.spid

------解决方案--------------------
SQL code

update a
set a.spbh=b.spbh_new
from a, b
where b.spid=a.spid

------解决方案--------------------
探讨
从楼主的数据看,spbh应该也要关联

------解决方案--------------------
SQL code

update A表
    set spbh = (select spbh_new from B表 where spid =A表.spid)