【问题标题】:Delete duplicate rows but keep one without having a uniqe column删除重复的行但保留一个而没有唯一的列
【发布时间】:2021-12-28 13:26:47
【问题描述】:

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

我已使用此查询来确定我有多少重复项:

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 列,而我拥有的数据结构并非如此。

【问题讨论】:

    标签: mysql sql magento2


    【解决方案1】:

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

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

    我认为这会起作用。

    【讨论】:

    • 我正要发这个!这确实有效。对我有用的完整查询是:alter ignore table catalog_url_rewrite_product_category add unique index tmp_remove_key(url_rewrite_id, category_id, product_id)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2022-11-03
    • 2016-09-18
    • 2019-02-23
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多