2016-12-20 83 views
0

我有一个令人困惑的问题。我的这样的查询;Mysql插入,重复键更新与子查询null问题

insert into simple_products 
(Product_Id,attribute,quantity,Barcode) 
values ((select id from products where sku='180 JK101G' and attribute='4621'),26,2,1068215) 
ON DUPLICATE KEY UPDATE 
attribute=values(attribute), 
quantity=values(quantity), 
Barcode=values(Barcode) 

但有时子查询不会返回任何内容。所以我想要什么都不做,但我找不到任何解决方案。

有没有办法呢?

+0

失去了支架和放,26,2,1068215选择查询内。 尝试此 插入到simple_products (产品,属性,数量,条形码) 值(选择从产品ID,26,2,1068215其中SKU = '180 JK101G' 和属性= '4621') ON DUPLICATE KEY更新 属性=值(属性), 数量=值(数量), 条形码=值(条形码) – IvanM

+0

是的,它的作品,但litle变化; – Humanwere

+0

插入到simple_products(product_Id,属性,数量,条形码)中选择id,26作为a,2作为b,1068215作为来自产品的c,其中sku ='180 JK101G'和属性='4621'ON DUPLICATE KEY UPDATE attribute = values属性),数量=价值(数量),条码=价值(条码) – Humanwere

回答

0

您正在寻找insert ... select

insert 
    into simple_products (Product_Id, attribute, quantity, Barcode)   

     select id, 26, 2, 1068215 
     from products 
     where sku='180 JK101G' 
      and attribute='4621' 

    on duplicate key update 
     attribute=values(attribute), 
     quantity=values(quantity), 
     Barcode=values(Barcode); 
0

它解决了由于@IvanM

insert into simple_products (Product_Id,attribute,quantity,Barcode) 
select id,26 as a,2 as b ,1068215 as c from products where sku='180 JK101G' and attribute='4621' 
ON DUPLICATE KEY UPDATE attribute=values(attribute), quantity=values(quantity), Barcode=values(Barcode)