mysql - sql UPDATE SET WHERE 条件给出错误 1242
问题描述
我收到错误 1242 子查询返回超过 1 行
如果产品包含这些添加,我想要更新列 bio 并将其设置为“bio-plant”。
UPDATE product
SET bio = 'bio-plant'
WHERE ( SELECT distinct product.Id_Product
FROM product , product_has_additions
WHERE product_has_additions.code IN ('E170', 'E220', 'E296', 'E300', 'E306', 'E322', 'E330', 'E333', 'E334', 'E335', 'E336', 'E341', 'E392', 'E400', 'E401', 'E402', 'E406', 'E407', 'E410', 'E412', 'E414', 'E415', 'E422', 'E440', 'E464')
AND product.Id_Product = product_has_additions.Id_Product
);
解决方案
我认为你真正需要的是两个表的连接:
UPDATE product p
INNER JOIN product_has_additions a
ON a.Id_Product = p.Id_Product
SET p.bio = 'bio-plant'
WHERE a.code IN (
'E170', 'E220', 'E296', 'E300', 'E306', 'E322', 'E330', 'E333',
'E334', 'E335', 'E336', 'E341', 'E392', 'E400', 'E401', 'E402',
'E406', 'E407', 'E410', 'E412', 'E414', 'E415', 'E422', 'E440', 'E464'
)
或者,使用EXISTS
:
UPDATE product p
SET p.bio = 'bio-plant'
WHERE EXISTS (
SELECT 1 FROM product_has_additions a
WHERE a.Id_Product = p.Id_Product
AND a.code IN (
'E170', 'E220', 'E296', 'E300', 'E306', 'E322', 'E330', 'E333',
'E334', 'E335', 'E336', 'E341', 'E392', 'E400', 'E401', 'E402',
'E406', 'E407', 'E410', 'E412', 'E414', 'E415', 'E422', 'E440', 'E464'
)
)
推荐阅读
- dataweave - DataWeave:csv 到 ascii 表
- c# - c# 如何将相同的对象添加到不同的面板?
- html - 如何在 html 和 css 中的图像前面放置以便阅读?
- javascript - Floating Point Number is greater than another floating point by how many points
- swagger-codegen - 我怎样才能使构建失败?
- amazon-web-services - 使用 Terraform 设置 CodePipeline
- r - 在 DESeq2 PCA 上为不同的 geom_point 形状添加黑色轮廓
- pandas - 合并具有不相等行的 Pandas 数据框
- c - 是否有替代方法来检索下面代码中的字符串数据?
- javascript - Nodejs Express 简单导入js文件路由器