Thursday, August 20, 2020

Magento 2.4 - MySQL query for product with attributes

 

Following mysql query will return SKU, product name, its type with size and color as attributes. 

SELECT sku, product_name, type_id

, MAX(CASE WHEN attribute='Color' THEN value END) color

, MAX(CASE WHEN attribute='Size' THEN value END) size

, MAX(CASE WHEN attribute='Visibility' THEN attrib_value END) visibility

, MAX(CASE WHEN attribute='Enable Product' THEN attrib_value END) enabled

from

(

select

p.entity_id, cv.value as product_name, p.type_id, p.sku, a.frontend_label as attribute, av.value as attrib_value, ao.value

from catalog_product_entity p


INNER JOIN catalog_product_entity_varchar cv ON cv.entity_id = p.entity_id AND 

cv.attribute_id=(SELECT attribute_id FROM eav_attribute ea JOIN eav_entity_type et 

ON  et.entity_type_code='catalog_product'  AND et.entity_type_id=ea.entity_type_id  

                                                        WHERE ea.attribute_code='name')

left join catalog_product_entity_int av on  p.entity_id = av.entity_id

left join eav_attribute a on av.attribute_id = a.attribute_id

left join eav_attribute_option_value ao on av.value = ao.option_id 


) as PROD


group by sku, product_name, type_id

having type_id = 'simple';