Update fields with multiple values from different tables in MySQL

0

I have 3 tables:

Product_categories (created from the import of a .csv )

product_reference | category_id | subcategories

REF.001           | 1           | 1/2

Categories (created from the import of a .csv )

id | name

1  | categoria_ejemplo_1
2  | categoria_ejemplo_2

Importer * (created to store all the data I need from the previous tables)

sku     | category

REF.001 | null

How could I update Importer to make it look like this?

sku     | category

REF.001 | categoria_ejemplo_1>categoria_ejemplo_2

* The data in the Importer table would be to export in .csv and be able to import them into WooComerce, hence you need to import the categories in the aforementioned way. link

    
asked by JuanjoNC 21.05.2018 в 13:52
source

1 answer

0

Solved:

UPDATE Importer i JOIN ( SELECT pc.product_reference,GROUP_CONCAT(c.name ORDER BY n.n SEPARATOR '>') category FROM (SELECT 1 n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) n JOIN Product_categories pc JOIN Categories c ON SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT('',pc.subcategories,'////'),'/',n.n),'/',-1) = c.id GROUP BY pc.product_reference ) d SET i.category = d.category WHERE i.sku = d.product_reference;

    
answered by 22.05.2018 в 12:45