根据一个表的字段,更新另外一个表的字段

时间:2023-01-26 17:12:33

update Table_A set LatestTM = U.TM, LatestData = U.Data from 
(
 select * from Table_B inner join 
 (  
  select MAX(TM) NEWESTTM from Table_B GROUP BY STCD
 )
 V on DRP.TM = V.NEWESTTM
) U WHERE Table_A.ID = U.ID                   

 

A表中假如有三个ID对象,LatestTM,LatestData表示三个对象最新时刻的最新状态信息,其值是由B表中检索更新而来,B表中不断增加的是三个ID对象的最新状态和对应的时刻,通过检索B表中每个ID对象最新时刻的最新数据信息去更新A表。

 

update [Dealers] set dianjilv = b.dianji from
(

select count(dealerid) as dianji,dealerid
  from [TongjiDealerPhoneClick]
  group by dealerid

) as b

where id=b.dealerid