user3835327
user3835327

Reputation: 1204

How to data in a table from another table?

How can i update 1 table value based on another table ID match ? for example below sql, i want update to tb_test NAME to tb_test2 NAME2 where condition code are same A001

enter image description here

expected result after update tb_test NAME will become ALI where condition code are A001

What i've tried so far based on online solution. (Failed to apply)
update tb_test set tb_test.name = tb_test2.name2 from tb_test A inner join tb_Test2 B on A.code = B.code2

Upvotes: 0

Views: 56

Answers (3)

Shadow Walker
Shadow Walker

Reputation: 206

The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected. (w3schools.com/sql/sql_insert_into_select.asp)

Upvotes: 0

data_henrik
data_henrik

Reputation: 17118

Something like the following should do:

update tb_test set name=(select tb2.name2 from tb_test2 tb2 where tb2.code2=code)

You update the name which is selected from the other table and the code/code2 columns need to match.

Upvotes: 1

Rahul Babu
Rahul Babu

Reputation: 790

The correct script would be :

update tb_test inner join tb_test2 on tb_test.CODE = tb_test2.CODE2 set tb_test.name = tb_test2.NAME2 ;

Upvotes: 0

Related Questions