0

So I want to run a query that add a bunch of product using SKU number (comma separated) into a specific category ID number 21.

How do i do that? I know how to move them before category using this query. I like to know how to use the comma separated sku in the query, to move them into a category.

INSERT INTO 
    catalog_category_product( category_id, product_id, position ) 
    (
        SELECT 652 , product_id, position
        FROM catalog_category_product
        WHERE category_id IN ( 650 )
    )
ON DUPLICATE KEY UPDATE category_id = 652

Any help would be appreciate it.

1 Answer 1

0

If I understand you correctly the query you are looking for is something along the lines like the one below.

I have taken it one step further and made sure that position is incremented and that you can choose the order in which you want the SKU's to show up.

SET @r = 0;

INSERT IGNORE INTO catalog_category_product (category_id, product_id, position)
SELECT 21, entity_id, @r:= (@r+1) FROM catalog_product_entity 
WHERE sku IN ('msj001', 'msj000') 
ORDER BY FIELD(id, 'msj001', 'msj000');
2
  • So how do I ignore when there is duplicate entries in the category that i am inserting in? is there a query i can add on to this current query to tell it to ignore?
    – Justin
    Commented Jun 21, 2015 at 5:05
  • I have gone and adjusted the query so as to not fail on duplicate keys but rather ignore the duplicate and continue on.
    – WilhelmE
    Commented Jun 22, 2015 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.