2014-01-23 79 views
1

這點我是肯定會的工作我的更新語句,是這樣的:爲什麼我的MySQL更新語句,涉及2個表不起作用?

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個或更多。這怎麼可以重寫,所以它會工作?我必須使用連接嗎?

+1

^h您是否嘗試將'products_id ='更改爲'products_id in'? – Dan

回答

3

而應該在使用中=

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 
2

試試這個

UPDATE products SET manufacturers_id = 37 where products_id in 
(select products_id from  products_to_categories where categories_id = 35); 

變化=IN

相關問題