选择加入的MYSQL更新

时间:2014-03-26 10:13:16

标签: mysql sql

早上好,

我已经编写了一个SQL查询来从我想要更新的数据库中获取所有数据但是可以解决我如何在这里添加更新语句。

SELECT product_id, product_title, prodcat_category_id, category_active, product_active from products 
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id 
LEFT JOIN categories on productcategories.prodcat_category_id =  categories.category_id 
WHERE category_active = 0

enter image description here

我想更新所有这些结果以使product_active为0,但我从未在select连接中完成更新语句。

2 个答案:

答案 0 :(得分:1)

不使用连接选择,而是使用连接进行更新。

update products
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id 
LEFT JOIN categories on productcategories.prodcat_category_id =  categories.category_id 
set product_active = 0
WHERE category_active = 0

答案 1 :(得分:1)

尝试以下查询

Update products set product_active = 0 where product_id IN 
(
SELECT product_id from products 
LEFT JOIN productcategories on products.product_id = productcategories.prodcat_product_id 
LEFT JOIN categories on productcategories.prodcat_category_id =  categories.category_id
WHERE category_active = 0
);