SQL两表之间:根据一个表的字段更新另一个表的字段

1. 写法轻松,更新效率高:
update table1
set field1=table2.field1,
field2=table2.field2
from table2
where table1.id=table2.id
    

update chq_gx_empinfo  set
                deptzz = chq_gx_empinfo2.deptzz,deptname =chq_gx_empinfo2.deptname
                from chq_gx_empinfo2 where chq_gx_empinfo.empno = chq_gx_empinfo2.empno
                and chq_gx_empinfo2.empno in ('CHQS2076','CHQS1959')
 

2. 常规方式,种写法相当于一个 Left join, 以外面的where为更新条数,如果不加where就是所有记录

update table1
set field1=(select top 1 field1 from table2 where table2.id=table1.id)
where table1.id in (condition)

posted @ 2018-11-22 15:19  lanesp  阅读(2382)  评论(0编辑  收藏  举报