简体   繁体   中英

How to update a field of a table that is inner join of another table in MySQL?

I have two tables: tablea and tableb . Both of them have three columns called columna , columnb , columnc .

Now I want to do this:

If tablea.columnb equals tableb.columnb ,then set tablea.columnc = tableb.columnc . I have written the sql and it works well, but I think that there must be a better way to do this? Can anyone help me to optimize my sql statement, or is there any other way?

UPDATE tablea ta 
SET 
ta.columnc = (
              SELECT columnc FROM tableb
              WHERE ta.columnb = tableb.columnb
             )
WHERE ta.columnb IN (
                     SELECT columnb FROM tableb
                     WHERE ta.columnb = tableb.columnb
                    )

您可以尝试这个简单的查询

Update tablea ta,table tb set ta.columnc=tb.columnc where ta.columnb =tb.columnb;

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