MySQL将其他表中的id值插入到匹配的行中

时间:2014-08-30 19:54:41

标签: mysql sql database-normalization

我想规范化我的表格:

products
id | name | manufacturer | manufacturer_id

manufacturers
id | name

我需要查询来更新products.manufacturer_id,其中products.manufacturer = manufacturers.name(我将删除products.manufacturer)

非常感谢你的时间

汤姆

2 个答案:

答案 0 :(得分:3)

您可以尝试这样:

update products INNER JOIN manufacturers
set products.manufacturer_id = manufacturers.id
where products.manufacturer = manufacturers.name

答案 1 :(得分:1)

update products p
   set p.manufacturer_id =
       (select m.id from manufacturers m where m.name = p.manufacturer)