Ismail
Ismail

Reputation: 17

Mysql insert,on duplicate key update with subquery null issue

I have a confusing problem. My query like this;

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)

But sometimes subquery returns nothing. So I want then do nothing but I can't find any solution.

Is there a way for this?

Upvotes: 0

Views: 378

Answers (2)

Ismail
Ismail

Reputation: 17

It's solved thanks to @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)

Upvotes: 0

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39981

You are looking for 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);

Upvotes: 1

Related Questions