I was asked recently to provide a feed from Magento which would provide a list
of all active SKUs, with product names, descriptions, urls and a comma separated
list of categories by name.
I assembled a query in MySQL. I grabbed all the products from
catalog_product_entity and joined it with catalog_product_entity_int to
determine if the products are enabled—pretty standard operation. Then after
doing this I joined again to the catalog_product_flat table for the
appropriate store ID since it’s easier to pull product information from than
joining to the same varchar table 150 times. I ran into a small problem when it
came down to getting the category names.
The categories in magento are stored as a comma separated list of entity_ids
in the cache tables, not a comma separated list of category names.
I worked a bit on it until I ended up with a query that was capable of getting a
comma separated list of names for a single row. It looked something like
This is where I thought, “wow MySQL makes it so easy to work with comma
separated lists”—WRONG
While getting this list was simple, iterating over a result set with this query
was not a possibility because when the field returns the values it returns them
as a string. In other words:
I pulled the following query from slickdev after searching for a
number of hours:
then adapted it for my needs and it worked. The query takes about 5 minutes to
run after all the modifications I made.