更新两个表我们得到重复

时间:2014-10-13 04:17:13

标签: sql

我有两张表,比如

emp

id  name sal deptno
-------------------
1    a    x   10
2    b    x   20
3    c    x   20

dept

id sal deptno
-------------
1  100 10
2  200 20
3  300 20

在此,我想更新sal表格中的emp

update emp
set sal = d.sal
from emp as e
left join dept as d on e.deptno = d.deptno

执行该查询后,我正在

id name sal deptno
------------------
1  a    100 10
2  b    200 20
3  c    200 20

但我想要

id name sal deptno
------------------
1   a   100 10
2   b   200 20
3   c   300 20

1 个答案:

答案 0 :(得分:1)

试试这个:

 update emp
 set sal = d.sal
 from emp as e
 left join dept as d on e.id = d.id