简体   繁体   English

如何更新一个表的字段,该表是MySQL中另一个表的内部联接?

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

I have two tables: tablea and tableb . 我有两个表: tableatableb Both of them have three columns called columna , columnb , columnc . 它们都具有称为columnacolumnbcolumnc三列。

Now I want to do this: 现在我要这样做:

If tablea.columnb equals tableb.columnb ,then set tablea.columnc = tableb.columnc . 如果tablea.columnb等于tableb.columnb ,则设置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? 我已经编写了sql,它运行良好,但是我认为必须有更好的方法来执行此操作? Can anyone help me to optimize my sql statement, or is there any other way? 谁能帮助我优化我的sql语句,或者还有其他方法吗?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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