选择具有重复的记录

时间:2010-06-09 13:57:04

标签: mysql sql duplicates

我想从表{(1}})中选择names不唯一的所有记录。我最好删除所有重复的记录。

如何做到这一点?假设我想要多次重新运行一个查询,直到它退出为止。

4 个答案:

答案 0 :(得分:3)

要查找哪些姓氏有重复项:

  SELECT lastname, COUNT(lastname) AS rowcount 
    FROM table 
GROUP BY lastname 
  HAVING rowcount > 1

删除所有姓氏的副本之一。运行直到它什么都不做。不是很优雅。

DELETE FROM table 
 WHERE id IN (SELECT id 
                FROM (SELECT * FROM table) AS t 
            GROUP BY lastname 
              HAVING COUNT(lastname) > 1)

答案 1 :(得分:2)

删除重复记录的最快速最简单的方法是发出一个非常简单的命令。

ALTER IGNORE TABLE [TABLENAME]添加唯一索引UNIQUE_INDEX([FIELDNAME])

这将锁定表格,如果这是一个问题,请尝试:

从table1 t1,table2 t2中删除t1 table1.duplicate_field = table2.duplicate_field(如果需要则添加更多,例如。和table.duplicate_field2 = table2.duplicate_field2)
和table1.unique_field> table2.unique_field
并分解成范围以更快地运行

答案 2 :(得分:0)

dup How can I remove duplicate rows?

DELETE names
FROM names
LEFT OUTER JOIN (
   SELECT MIN(RowId) as RowId, lastname 
   FROM names
   GROUP BY lastname 
) as KeepRows ON
   names.lastname = KeepRows.lastname 
WHERE
   KeepRows.RowId IS NULL

假设:您有一个RowId

答案 3 :(得分:0)

SELECT COUNT(*) as mycountvar FROM names GROUP BY lastname WHERE mycountvar > 1;

然后

DELETE FROM names WHERE lastname = '$mylastnamevar' LIMIT $mycountvar-1

但是:你为什么不把旗帜“姓氏”als标记为独特的,所以复制品不可能进来?