2016-12-20 81 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)