どのようにちょうど最初の1を維持するすべての重複画像を削除するには?

ムーサ:

私はこのテーブルを持っています:

product_images

id - product_id - image - main_image - date

画像:

ここでは、画像の説明を入力します。

このテーブルには、私の製品の全体像が含まれています。

私はすべての重複を削除するためにSQLでクエリを必要なものproduct_idと、画像が含まれていることは、上記の画像である非常に最初のものを保ちます2019/03/9g.jpg

郵便番号:

このために存在していない場所を使用します。

これは、以下削除するリストを与えます

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
)

これは、以下のすべてが、各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
) 

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=277710&siteId=1