Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating two tables we getting duplicates

Tags:

sql

I have two tables like

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

Here I want to update sal column in emp table

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

After executing that query I am getting

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

but I want

id name sal deptno
------------------
1   a   100 10
2   b   200 20
3   c   300 20
like image 944
Subbaramireddy chejerla Avatar asked Jan 23 '26 07:01

Subbaramireddy chejerla


1 Answers

try this one :

 update emp
 set sal = d.sal
 from emp as e
 left join dept as d on e.id = d.id
like image 55
Asromi rOmi Avatar answered Jan 25 '26 22:01

Asromi rOmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!