我确信可行的更新声明是这样的:
UPDATE products SET manufacturers_id = 37 where products_id =
(select products_id from products_to_categories where categories_id = 35);
但是在PHPMyAdmin中,我得到的错误是:
#1242 - Subquery returns more than 1 row
但我希望更新1行以上。我试图在一个声明中更新100或更多。如何重写这样才能起作用?我必须使用加入吗?
答案 0 :(得分:3)
您应该使用IN而不是=
UPDATE
products
SET
manufacturers_id = 37
WHERE
products_id IN (SELECT products_id
FROM products_to_categories
WHERE categories_id = 35);
或者您可以使用JOIN
UPDATE
products INNER JOIN products_to_categories
ON products.product_d = products_to_categories.products_id
SET
products.manufacturers_id = 37
WHERE
products_to_categories.categories_id = 35
答案 1 :(得分:2)
试试这个
UPDATE products SET manufacturers_id = 37 where products_id in
(select products_id from products_to_categories where categories_id = 35);
将=
更改为IN