我有下面的表1:EMP
ID NAME CITY AMT
-------------------------------------------
1 sajani Bangalore 0
2 Prashanth Bangalore 0
3 Jayvin Bangalore 0
表2:EMP1
ID NAME1 CITY1 AMT1
---------------------------------------------
1 Sajani Bangalore 10
1 Sajani Bangalore 10
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
ID是密钥,在两个文件中都是通用的。我希望UpdateSQL使用表2的amount字段更新表1的amount字段,结果如下。
结果:
ID NAME CITY AMT
--------------------------------------------
1 sajani Bangalore 20
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
一种方法是相关的子查询:
update emp
set amt = (select sum(e1.amt) from emp1 e1 where e1.id = emp.id);
您可以使用检查值是否匹配
update emp
set amt = (select sum(e1.amt) from emp1 e1 where e1.id = emp.id);
where amt <> (select sum(e1.amt) from emp1 e1 where e1.id = emp.id);