如何根据ID匹配更新表列?

时间:2016-01-18 07:25:04

标签: mysql

applicants.sql

full name | company Id    |      CompanyLabel
Johny Doe |     1         |    
Johny ben |     2         |  

companylist.sql

companyid  | Company name     | CompanyLabel
1   | Starks Industry  | Starks Industry - New York
2   | Captain Industry | Captain Industry - Paris
问题是我有: 5000名没有CompanyLabel的申请人 1000家不同的公司。

我用这个来获得CompanyLabel 1 by 1

UPDATE applicants set CompanyLabel = 'Starks Industry - New York' WHERE companyid = 1

如何在一次查询中执行所有操作?

1 个答案:

答案 0 :(得分:0)

您可以使用加入并更新某些内容

update applicants a
join companylist c on c.companyid = a.companyid
set a.CompanyLabel = c.CompanyLabel