从select sql创建更新查询

时间:2017-02-23 11:05:36

标签: mysql sql

我做了一个select查询,找出哪些产品没有组合时有引用集,并希望从结果中将该值更新为NULL。

任何人都知道如何构建更新查询?

SELECT QUERY:

SELECT 
  `id_product_supplier` , `id_product` , `id_product_attribute` ,
  `product_supplier_reference` , COUNT( `product_supplier_reference` )
FROM `ps_product_supplier`
GROUP BY `id_product`
HAVING COUNT( `product_supplier_reference` ) >1
AND `id_product_attribute` =0
AND `product_supplier_reference` >0

我希望从结果中获得product_supplier_reference = NULL的更新。

1 个答案:

答案 0 :(得分:0)

Try below script 

 UPDATE ps_product_supplier SET product_supplier_reference = --Value
 FROM
 (
   SELECT  id_product_supplier , id_product , id_product_attribute ,   
   product_supplier_reference , COUNT( product_supplier_reference ) 
   FROM ps_product_supplier 
   GROUP BY id_product 
   HAVING COUNT( product_supplier_reference ) >1 AND id_product_attribute =0      
   AND product_supplier_reference >0
 ) A WHERE --Your Codition