簡體   English   中英

mysql更新來自另一個表的數據,並在兩個字段上進行聯接

[英]mysql update with data from another table with join on two fields

我需要使用來自兩個字段匹配的另一個數據更新1個表。 我有一個查詢,但它只是鎖定。

我有一個employee_training_courses表我有一個company_training_categories表

我需要從company_training_categories中獲取ID,其中兩個表中的名稱和account_id都相同。

到目前為止,我有這個...

update employee_training_courses tc join company_training_categories ctc on ctc.name = tc.name AND ctc.account_id = tc.account_id set tc.company_training_category_id = ctc.id;

我可以讓查詢繼續運行,但是顯然它掛在了某個地方!

這是您的查詢:

update employee_training_courses tc join
       company_training_categories ctc
       on ctc.name = tc.name AND ctc.account_id = tc.account_id
    set tc.company_training_category_id = ctc.id;

這是一個非常合理的查詢。 您可能只需要索引即可加快速度。 我建議:

create index idx_company_training_categories_2 on company_training_categories(name, account_id)

甚至:

create index idx_company_training_categories_3 on company_training_categories(name, account_id, id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM