Get full list of categories using sql

Using @mpaepper answer I was able to get a query that seems to be pulling the correct information back. I was able to test it on two different magento databases and each looked right. Anyway here it is.

  SELECT DISTINCT cc.entity_id as id, cc.value as path, cc1.value as name    
  FROM catalog_category_entity_varchar cc    
  JOIN catalog_category_entity_varchar cc1 ON cc.entity_id=cc1.entity_id    
  JOIN eav_entity_type ee ON cc.entity_type_id=ee.entity_type_id
  JOIN catalog_category_entity cce ON cc.entity_id=cce.entity_id
  WHERE cc.attribute_id = '57' AND cc1.attribute_id = '41' AND ee.entity_model = 'catalog/category'; 

I probably didn't need to join eav_entity_type but I will probably be using this query again on different versions of magento so I think this will help keep the query reusable.


Ok, so here are the tables and what you will need to do (I will let you do the MySQL join fun for yourself ;)):

catalog_category_entity is the base table which has your category ID (entity_id).

You then need to identify the attribute IDs for name and url_path from the table eav_attribute.

In my case, the attribute_code name for entity_type_id 3 (for me 3 is categories, look that up in the table eav_entity_type) has the attribute_id 41. In my case, the attribute_code url_path for entity_type_id 3 has the attribute_id 57.

Both name and url_path are of type varchar, so you will find their values in catalog_category_entity_varchar (filter for attribute_id and entity_id where entity_id is the corresponding ID of the category).

Thus you need to use the catalog_category_entity table and join catalog_category_entity_varchar twice with the entity_id as join condition and specifying the attribute_ids which you can look up. Alternatively, you can do more joins, so you do not look up the IDs before, but join them in.

Have fun! :)


SELECT DISTINCT cc.entity_id as category_id, cc.value as name, cc1.value as 
url_path ,cce.parent_id as parent_id ,0 as top,level as `column`,position as 
sort_order,1 as status,created_at as date_added,updated_at as date_modified 

FROM catalog_category_entity_varchar cc 
JOIN catalog_category_entity_varchar cc1 ON cc.entity_id=cc1.entity_id   
JOIN catalog_category_entity_int cc_int ON cc1.entity_id=cc_int.entity_id   
JOIN eav_entity_type ee ON cc.entity_type_id=ee.entity_type_id JOIN catalog_category_entity cce ON cc.entity_id=cce.entity_id 
WHERE cc.attribute_id in (select attribute_id from eav_attribute where attribute_code ='name') AND cc1.attribute_id in (select attribute_id from eav_attribute where attribute_code ='url_path')
and cc_int.attribute_id in (select attribute_id from eav_attribute where attribute_code ='is_active')
and cc_int.value = 1
and ((cce.parent_id = 2 and cce.children_count > 1) or cce.parent_id > 2)       
AND ee.entity_model = 'catalog/category' order by cce.parent_id asc, 
cce.position asc;

Tags:

Sql

Category