How to delete all duplicate image just keep the first one?

Musa :

I have this table:

product_images

id - product_id - image - main_image - date

image:

enter image description here

This table contains the whole image of my products.

What I need a query in SQL to remove all duplicate product_id and keep the very first one which is in the image above that contains an image 2019/03/9g.jpg

zip :

use where not exists for this:

This below gives the list to delete

select *
from product_image a
where exists
(
select 1 from product_image b 
where a.product_id = b.product_id and a.id < b.id
)

This below delete all but the first for each product_id

    delete
    from product_image 
    where id in
    ( 
    select id from
    (
      select id 
      from product_image a
      where exists
      (
      select 1 from product_image b 
      where a.product_id = b.product_id and a.id < b.id
      )
    )a
) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=217383&siteId=1