SqlServer两表之间:根据一个表的字段更新另一个表的字段 | 您所在的位置:网站首页 › oracle更新表字段时更新另一个字段 › SqlServer两表之间:根据一个表的字段更新另一个表的字段 |
经百度查询到如下两种写法: 1. 写法轻松,更新效率高: update table1 set field1=table2.field1, field2=table2.field2 from table2 where table1.id=table2.id 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) ———————————————— 当要更新的字段是另外两张表的结果集时,需要用到with as ,下面是我自己用到的sql: with table1 as ( select e.emp_id,t.health_attr_id from sys_emp e inner join sys_health_type t on e.health_type_id = t.health_type_id ) update a set [health_attr_id] = b.[health_attr_id] from dept_emp_time a inner join table1 b on a.emp_id = b.emp_id |
CopyRight 2018-2019 实验室设备网 版权所有 |