Question

I recently did a migration from M1 to M2, and with doing that, all images came along. Some products tend to have north of 10 images, and some more like 1-3.

I am trying to find a simple and fast way of getting a list of products that would say for example have more than 5 images in their image collection. I don't need to view the images or see what the simple/base/thumbnail are. I just want the total count per product.

Is there a simple SQL query or php block I could run for this?

Was it helpful?

Solution

The following query returns the number of images per product:

SELECT sku,count(*) FROM `catalog_product_entity` AS a 
LEFT JOIN `catalog_product_entity_media_gallery_value_to_entity` AS b ON a.entity_id = b.entity_id 
group by sku

I am not sure if it works properly in case of complex products or bundle. But it gives you an idea about how to proceed.

More generally, you should use the following Magento tables:

  • catalog_product_entity (for product ids)
  • catalog_product_entity_media_gallery_value_to_entity (for image and product relationship)
  • catalog_product_entity_media_gallery (for images path)

OTHER TIPS

The comment above is almost correct, but it counts the product with 0 images as one due to the count(*)

SELECT product.sku,count(gallery.value_id) FROM `catalog_product_entity` AS product
LEFT JOIN `catalog_product_entity_media_gallery_value_to_entity` AS gallery
ON product.entity_id = gallery.entity_id
GROUP BY product.sku

If you found this thread, because you are also want to find products without images:

SELECT product.sku,count(gallery.value_id) FROM `catalog_product_entity` AS product
LEFT JOIN `catalog_product_entity_media_gallery_value_to_entity` AS
gallery ON product.entity_id = gallery.entity_id
GROUP BY product.sku
HAVING count(gallery.value_id) = 0
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top