2

I have this SQL query to get all Magento Categories.

SELECT DISTINCT
    cc.entity_id AS id,
    cc.`value` AS path,
    cc1.`value` AS `NAME`,
    cce.`level`,
    cce.parent_id
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'

This returns all categories except the one I created a new category from Magento backend but that is not showing.

That category is published and has no products in it. Following image is from catalog_category_entity_varchar table.

enter image description here

entity_id = 449 is showing when I run that query, because it has attribute_id = 57 and 41

But I am talking about entity_id = 452 that is not showing, because it does not have attribute_id = 57.

I want to ask Magento experts, what does attribute_id = 57 belong to? and how can I fix this query to fetch all categories? PS I want Pure SQL query, No Magento code!

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146

2 Answers2

1

Just a guess...

SELECT DISTINCT cc.entity_id id
              , cc.value path
              , cc1.value NAME
              , cce.level
              , cce.parent_id
           FROM catalog_category_entity_varchar cc
           LEFT
           JOIN catalog_category_entity_varchar cc1 
             ON cc.entity_id = cc1.entity_id
            AND cc1.attribute_id = 41
           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 ee.entity_model = 'catalog/category'
Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • It also returned same number of results :( didnt return that category that I am missing – Umair Ayub Jul 25 '16 at 08:48
  • @AndhiIrawan unless you're going for the archaeologist badge, I'm not sure that there's much merit in commenting on posts from 5 years ago. – Strawberry Apr 19 '21 at 07:09
1

You are selecting categories from EAV category model which has attributes 57 and 41 of varchar type :

cc.attribute_id = '57'
cc1.attribute_id = '41'

According to my 1.9 magento installation this are name and path attributes of catalog/catagory:

select distinct ea.attribute_code from eav_attribute as ea inner join catalog_category_entity_varchar as vc on ea.attribute_id=vc.attribute_id where vc.attribute_id in (57,41);

To get all raw categories use this sql:

SELECT `e`.* FROM `catalog_category_entity` AS `e` WHERE (`e`.`entity_type_id` = '3')'

or to get categories with names use this:

SELECT `e`.*,
       IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name`
FROM `catalog_category_entity` AS `e`
INNER JOIN `catalog_category_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`)
AND (`at_name_default`.`attribute_id` = '41')
LEFT JOIN `catalog_category_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`)
AND (`at_name`.`attribute_id` = '41')
Ilja
  • 1,205
  • 1
  • 16
  • 33