从另一个表条件更新表格字段

时间:2017-09-27 10:44:50

标签: mysql

我有这个查询,我的目的是更准确地更新产品表“模型”字段,使用Model表中的数据,这里的条件是获取模型名称,从模型表和品牌中查看id是什么产品表中的名称

UPDATE products
SET products.model = (SELECT Model.id FROM Model, Brand
                      WHERE Brand.id = Model.id_brand
                          AND Brand.title = 'ALCATEL'
                          AND Model.title = products.model)
WHERE products.brand = 'ALCATEL'

1 个答案:

答案 0 :(得分:0)

update products set products.model = (select Model.id from Model, Brand where Brand.id = Model.id_brand and Brand.title = 'ALCATEL' and Model.title = products.model) where products.brand = 'ALCATEL'

在您的子查询中,您只能返回一列,并且强制它只返回单个值,因此需要使用LIMIT 1.以下是修改后的子查询。

select Model.id from Model INNER JOIN Brand ON Brand.id = Model.id_brand where Brand.title = 'ALCATEL' and Model.title = products.model LIMIT 1