我有这张桌子:
product_images
id - product_id - image - main_image - date
image:
此表包含我产品的整个图像。
我需要在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 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
)
)
DELETE FROM product_images
WHERE id NOT IN (SELECT min(id)
FROM product_images
GROUP BY product_id);