简体   繁体   English

从选择SQL创建更新查询

[英]Create update query from select sql

I did a select query to find which products have a reference set when they not have a combination, and want to update that value to NULL from the result. 我进行了选择查询,以查找哪些产品没有组合时有参考集,并希望将结果从该值更新为NULL。

Anyone got an idea on how to build the query for the update? 任何人都有关于如何建立更新查询的想法吗?

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

I want to have an update that is product_supplier_reference = NULL from the result. 我要更新的结果是product_supplier_reference = NULL。

try this 尝试这个

update ps_product_supplier  tt inner join(
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)t on t.id_product_supplier=tt.id_product_supplier
set tt.product_supplier_reference=null 
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 

TRY THIS if you want to simply update the particular column then select only required column in select statement as below and join it for the update: 如果您只想更新特定的列,请尝试此操作 ,然后在如下所示的select语句中仅选择所需的列,然后将其加入以进行更新:

UPDATE `ps_product_supplier` pps
INNER JOIN (
        SELECT 
          `id_product`
        FROM `ps_product_supplier`
        WHERE `id_product_attribute` =0
        AND `product_supplier_reference` >0
        GROUP BY `id_product`
        HAVING COUNT( `product_supplier_reference` ) >1) p ON p.id_product = pps.id_product
SET pps.product_supplier_reference = NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM