第一种方法:

update tablea set column_name1=(select name2 from tableb where tableb.name3=tablea.name1)
只修改一个
update tablea set column_name1=(select name2 from tableb where tableb.name3='a') where tablea.name1='A'

第二种方法:

假设A表有字段ID和NameA,B表有字段ID和NameB,两个表通过ID连接,把NameB更新到NameA,可以这么写:
merge into A
using(select NameB fromB) TMP
on (A.ID=TMP.ID)
when matched then
update set A.NameA=TMP.NameB

第三种方法:(建议使用这个)

  • A表数据

    

  • B表数据

          

  • 现在要把B表 B_COSTS 的值update到A表 A_COSTS 字段
  • SQL语法:

      update a set (a.a_costs) = (select b.b_costs from b where a.id = b.id and a.a_id = b.b_id) where exists
                      (select 1 from b where a.id = b.id and a.a_id = b.b_id)

  • 结果:

    

第四种方法:(更新内嵌视图)

 

update (

select e.sal as emp_sal,  e.comm as emp_comm,  ns.sal as ns_sal,  ns.sal/2 as ns_comm 

from emp e,new_sal ns where e.deptno = ns.deptno

) set emp_sal = ns_sal,emp_comm = ns_comm;

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐