简体   繁体   中英

Update mysql table with select query from another database

I have two databases and I want to update one table with values from another database table. I am using the following query but it does not work.

UPDATE database1.table1
SET field2 = database2.table1.field2
WHERE database1.table1.field1 = database2.table1.field1

I have also tried the following query but it does not work either:

UPDATE database1.table1
SET field2 = "SELECT field2 FROM database2.table1"
WHERE database1.table1.field1 = database2.table1.field1

UPDATE 1

based on your comment , markup should be part of the join. Here's the correct one:

UPDATE oman.ProductMaster_T
    INNER JOIN main.ProductMaster_T 
        ON main.ProductMaster_T.ProductID = oman.ProductMaster_T.ProductID 
SET oman.ProductMaster_T.Markup = main.ProductMaster_T.Markup

you can even add an ALIAS to simplify the statement,

UPDATE oman.ProductMaster_T o
    INNER JOIN main.ProductMaster_T m 
        ON m.ProductID = o.ProductID 
SET o.Markup = m.Markup

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