如何删除所有重复的图像,只保留第一个?

问题描述 投票:-1回答:2

我有这张桌子:

product_images

id - product_id - image - main_image - date

image:

enter image description here

此表包含我产品的整个图像。

我需要在SQL中进行查询以删除所有重复的product_id,并保留上面图像中的第一个包含图像2019/03/9g.jpg的内容

mysql sql
2个回答
1
投票

在此不存在的地方使用:

此下面列出了要删除的列表

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 a
where product_id in
(
select product_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
)


0
投票
DELETE FROM product_images
WHERE id NOT IN (SELECT min(id)
                 FROM product_images
                 GROUP BY product_id);
© www.soinside.com 2019 - 2024. All rights reserved.