简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM