1

I have a database of magento with double images, I want to delete those but first i got to detect them with a sql query.

I have tried this code

select t1.VALUE from catalog_product_entity_media_gallery t1 
join catalog_product_entity_media_gallery t2 on (t1.value = t2.value)

this one:

  select * from catalog_product_entity_media_gallery where value=value 

and this one:

select
*
from
(
    select
        value
    from
        catalog_product_entity_media_gallery
    group by
        value
    having count(*) > 1
) as t
inner join catalog_product_entity_media_gallery on (
    catalog_product_entity_media_gallery.value = t.value
)

the first gives an error and the second- and third one gives back every product.

this is my small test database

mxix
  • 3,539
  • 1
  • 16
  • 23
Djeroen
  • 571
  • 6
  • 21

2 Answers2

0

Give this one a try:

select
*
from (
    select 
        entity_id,attribute_id,value,
        MIN(value_id) value_id
    from catalog_product_entity_media_gallery
    group by
        entity_id,attribute_id,value
    having COUNT(*) > 1
) A1
inner join catalog_product_entity_media_gallery A2 on
    A1.entity_id = A2.entity_id and
    A1.attribute_id = A2.attribute_id and
    A1.value = A2.value and
    A1.value_id = A2.value_id
mxix
  • 3,539
  • 1
  • 16
  • 23
0

You can just get the min id by value, then except the other records:

select
*
from catalog_product_entity_media_gallery t1
where exists
( select * from 
  (select  value, min(value_id) as min_value_id
    from   catalog_product_entity_media_gallery
    group by  value
  ) as t2
  where t1.value=t2.value and t1.value_id=t2.min_value_id
)

If you want delete the duplicated rows, change exists to not exists.

delete
from catalog_product_entity_media_gallery t1
where not exists
( select * from 
  (select  value, min(value_id) as min_value_id
    from   catalog_product_entity_media_gallery
    group by  value
  ) as t2
  where t1.value=t2.value and t1.value_id=t2.min_value_id
)
Jaugar Chang
  • 3,176
  • 2
  • 13
  • 23