簡體   English   中英

如何更新一個表的字段,該表是MySQL中另一個表的內部聯接?

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

我有兩個表: tableatableb 它們都具有稱為columnacolumnbcolumnc三列。

現在我要這樣做:

如果tablea.columnb等於tableb.columnb ,則設置tablea.columnc = tableb.columnc 我已經編寫了sql,它運行良好,但是我認為必須有更好的方法來執行此操作? 誰能幫助我優化我的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