AI Reports

Magento 2 SQL queries › Products and catalogue

What values does the color attribute have, and how many products use each?

Short answer: Options are in eav_attribute_option; products point to them from catalog_product_entity_int. Count per option, including the unused ones.

In the Magento admin

  1. Go to Stores › Attributes › Product, open color to see the options, then filter the product grid by each one.

With one query

These queries only read data. Run them on a copy of your database if you can, or ask your developer to. If your tables have a prefix (set in app/etc/env.php), add it to each table name.

Magento Open Source

SELECT o.option_id, lbl.value AS colour, COUNT(v.value_id) AS products
FROM eav_attribute_option o
JOIN eav_entity_type t ON t.entity_type_code = 'catalog_product'
JOIN eav_attribute a_color ON a_color.entity_type_id = t.entity_type_id AND a_color.attribute_code = 'color'
JOIN eav_attribute_option_value lbl ON lbl.option_id = o.option_id AND lbl.store_id = 0
LEFT JOIN catalog_product_entity_int v ON v.attribute_id = a_color.attribute_id AND v.value = o.option_id AND v.store_id = 0
WHERE o.attribute_id = a_color.attribute_id
GROUP BY o.option_id, lbl.value
ORDER BY products DESC;

Adobe Commerce

Product, category, rule and CMS values are stored per version and joined on row_id (why):

SELECT o.option_id, lbl.value AS colour, COUNT(e.row_id) AS products
FROM eav_attribute_option o
JOIN eav_entity_type t ON t.entity_type_code = 'catalog_product'
JOIN eav_attribute a_color ON a_color.entity_type_id = t.entity_type_id AND a_color.attribute_code = 'color'
JOIN eav_attribute_option_value lbl ON lbl.option_id = o.option_id AND lbl.store_id = 0
LEFT JOIN (catalog_product_entity_int v
           JOIN catalog_product_entity e ON e.row_id = v.row_id AND e.created_in <= UNIX_TIMESTAMP() AND e.updated_in > UNIX_TIMESTAMP())
       ON v.attribute_id = a_color.attribute_id AND v.value = o.option_id AND v.store_id = 0
WHERE o.attribute_id = a_color.attribute_id
GROUP BY o.option_id, lbl.value
ORDER BY products DESC;

Good to know

Related

Gaurav Harsh

Gaurav Harsh is a Magento developer who works on large Adobe Commerce stores and builds AI Reports, a Magento extension for asking your store's data questions in plain English. LinkedIn