mysql - 删除重复的行,但保留一个没有唯一列的行

标签 mysql sql magento2

我有一个包含重复条目的表,我想将其删除,只保留一个重复条目。正如您所看到的,它们在每一列中都完全相同,无法区分它们:

enter image description here

我使用此查询来计算我有多少重复项:

select url_rewrite_id, category_id, product_id, count(*) cnt
from catalog_url_rewrite_product_category
group by url_rewrite_id, category_id, product_id
having cnt > 1
order by cnt desc

我可以使用它的变体来删除所有重复项:

delete
from catalog_url_rewrite_product_category
where url_rewrite_id in (
    select url_rewrite_id
    from catalog_url_rewrite_product_category
    group by url_rewrite_id, category_id, product_id
    having count(*) > 1
)

我遇到的问题是它会删除所有重复的条目,并且不会保留最后一个。

之前的问题( herehere )假设有一个唯一的 id 列,但我所拥有的数据结构并非如此。

最佳答案

您是否尝试过我在您分享的线程 ( here ) 中读到的解决方案之一?

ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (site_id, title, company);

我认为这会起作用。

关于mysql - 删除重复的行,但保留一个没有唯一列的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70006588/

相关文章:

php - 帮助使用 MySQL 和 PHP 上传视频

php - 离线上网状态下Mysql的使用

sql - Snowflake 解析 JSON 时出错 : unfinished string, pos <number>

Magento2:每个商店的配置数据值错误

php - 如何根据 SQL 数据库中的日期触发 PHP 服务器中的更改

compiler-errors - 在 Magento 版本中运行 "composer update"时出现错误。 2.3.3,PHP 7.3.15

php - 在 shell 中运行连接到 mysql 数据库的 php 脚本

mysql - 如何从同一列表中选择包含 ID 而不是仅包含 ID 的记录

mysql - 根据自定义条件清除重复行

java - Hibernate @Formula 减法和加法