Jack Hillard
Jack Hillard

Reputation: 606

MYSQL Copy field from one table to another table

I have two MYSQL tables. One is 'orders' and another is 'users'. I just added a field to my 'orders' table called 'state'. I'd like to go through each record in my 'orders' table and find which state the order will be shipped to, then add that to the 'state' field.

The 'orders' table has a 'user' field that would match the 'id' field in my 'users' table.

I know how to do this programmatically with PHP but I'm sure it can be done with a simple SQL statement. I'm just not terribly great with SQL so this will help me learn.

Upvotes: 0

Views: 106

Answers (1)

Jessica
Jessica

Reputation: 7005

UPDATE `orders` o
LEFT JOIN `users` u
ON o.user = u.id
SET state = u.state

Upvotes: 4

Related Questions