sql MySQL删除多列上的dupes

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MySQL删除多列上的dupes相关的知识,希望对你有一定的参考价值。

# If you want to keep the row with the lowest id value:

DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name



# If you want to keep the row with the highest id value:

DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name
--Find Duplicates
 
SELECT t.ID, t.id_A, t.id_B
FROM (
  SELECT id_A, id_B
  FROM table_name
  GROUP BY id_A, id_B
  HAVING count(*) > 1
) x, table_name t
WHERE x.id_A = t.id_A AND x.id_B = t.id_B
ORDER BY t.id_A, t.id_B
 
--Delete duplicates
 
DELETE FROM table_name WHERE id IN (
    SELECT * FROM (
        SELECT t.ID
        FROM (
            SELECT id_A, id_B
            FROM table_name
            GROUP BY id_A, id_B
            HAVING count(*) > 1) x, table_name t
        WHERE x.id_A = t.id_A AND x.id_B = t.id_B
        ORDER BY t.id_A, t.id_B
    ) AS p
)

以上是关于sql MySQL删除多列上的dupes的主要内容,如果未能解决你的问题,请参考以下文章

sql 删除Dupes

SQL删除多列语句

索引(简介,创建,删除)

多列上的 SQL SUM 和 COUNT

从 SQL 表中删除重复的行(基于多列的值)

mysql 多列数据搜索出不重复的各项