删除列中的重复项

时间:2013-10-02 01:42:41

标签: mysql sql

我有以下查询基本上可以在我的用户名栏中找到所有重复项:

SELECT  `username` 
FROM  `instagram_user` 
GROUP BY  `username` 
HAVING COUNT( * ) >1

如何删除所有重复项,以便它只会在表中留下一个唯一的用户名?只要表中有一个唯一的用户名,我不关心它是持久化还是删除的实体。

3 个答案:

答案 0 :(得分:2)

如果您不关心选择哪条记录,则只需在使用IGNORE

时添加唯一约束
ALTER IGNORE TABLE instagram_user ADD UNIQUE (username);

这是 SQLFiddle 演示

并且MySQL将为您完成这项工作。无论如何,您希望拥有该唯一约束,以便将来保持您的表格不会重复。

或者你可以做

DELETE t
  FROM instagram_user t JOIN
(
  SELECT username, MAX(id) id
    FROM instagram_user
   GROUP BY username
  HAVING COUNT(*) > 1
) q 
    ON t.username = q.username
   AND t.id <> q.id

对于具有重复用户名的行,这一行只会留下一行最大ID。

这是 SQLFiddle 演示

答案 1 :(得分:0)

不确定这是否适用于SQL Server,您可以在mysql中尝试类似的代码。

;With CteUsers AS(

SELECT *,ROW_NUMBER() OVER (PARTITION BY username Order by username) AS ROWID
FROM(

SELECT PkId, `username` 
FROM  `instagram_user`
)tbltemp)
SELECT * FROM CteUsers; 

这将产生如下

PkId username RowId
1     xx        1
2     xx        2
....

然后删除RowId&gt;的位置1

;使用CteUsers AS(

    SELECT *,ROW_NUMBER() OVER (PARTITION BY username Order by username) AS ROWID
    FROM(

    SELECT PkId, `username` 
    FROM  `instagram_user`
    )tbltemp)
  DELETE instagram_user WHERE PkId iN (SELECT PkId FROM CteUsers  WHERE ROWID > 1);

答案 2 :(得分:0)

这将为您提供重复项(即您需要删除的副本)...

select a.id, a.username from instagram_user a, instagram_user b
where a.username = b.username and a.id <> b.id
  and b.id = (select min(id) from instagram_user where username = a.username)

所以DELETE就像......

delete from instagram_user where id in
 (select a.id from instagram_user a, instagram_user b
    where a.username = b.username and a.id <> b.id
      and b.id = (select min(c.id) from instagram_user c 
                  where c.username = a.username))