M
M
Mracula2022-01-26 15:57:20
opencart
Mracula, 2022-01-26 15:57:20

How to assign the main category to all products using mysql query?

How to assign the main category to all products using mysql query? The product is assigned categories (several) in which it is shown. All these categories have different nesting. It is necessary to assign to all products the main one from the youngest category. Opencart 2.3 (Russian assembly)
There is such a solution, but it does not work.

UPDATE c_product_to_category p2c0
JOIN ( 

SELECT p0.product_id,  
(SELECT p2c.category_id FROM oc_product p
JOIN oc_product_to_category p2c ON p.product_id = p2c.product_id
JOIN oc_category_path cp ON p2c.category_id = cp.category_id
WHERE p.product_id = p0.product_id
ORDER BY cp.level DESC
LIMIT 1) as cat
FROM oc_product p0) t0 
ON t0.cat =  p2c.category_id AND p2c0.product_id = t0.product_id
SET main_category = 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mracula, 2022-01-27
@Mracula

Found such a query, but it only assigns a category under a certain id

INSERT INTO oc_product_to_category( product_id, category_id, main_category )
SELECT product_id, '372', '0'
FROM oc_product

V
VVCh, 2022-02-02
@VVCh

1. You would look at the errors
2. You would ask a question to the person who gave you this request
3. the request itself is correct, but !!! it contains syntax errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question