JanLi
JanLi

Reputation: 71

How to update sql data in this condition?

i have table like this

user_id name age genre target
0000001 Roy 17 M X
0000002 Fina 21 F X

and i create new column in another table and want to move "target" column's value to that table

user_id colum_a target
0000001 aaaaaaa
0000002 aaaaaaa

please tell me how to use sql update to move the target value to Table 2 ? Thanks

Upvotes: 0

Views: 42

Answers (1)

Soheil Rahsaz
Soheil Rahsaz

Reputation: 781

If user_id is unique:

UPDATE tbl2 
SET target = (SELECT target FROM tbl1 WHERE tbl1.user_id = tbl2.user_id)

UPDATE: You can also do it with join:

UPDATE tbl2 
JOIN tbl1 ON tbl2.user_id = tbl1.user_id
SET tbl2.target = tbl1.target

Upvotes: 1

Related Questions