Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT followed by UPDATE

I have Employee table and HR table. Address of employee ( address column ) is there in both the tables. I want to update the HR table's address column with the address from Employee table, for all those records wherever there is a mismatch between the HR table's address and Employee table's address for the same employee.employee-id is primary key in employee table and foreign key in HR table.

like image 876
newbie Avatar asked Jul 29 '26 18:07

newbie


1 Answers

You can still do JOIN on UPDATE statements.

UPDATE  HR a
        INNER JOIN Employee b
            ON a.EmpID = b.ID  -- relationship column
SET     a.address = b.address
WHERE   a.address <> b.address -- to make this query faster
                               -- filter only those address that didn't match
like image 185
John Woo Avatar answered Aug 01 '26 07:08

John Woo



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!