删除具有两个重复值的值,而不管它们位于哪个列中

时间:2013-01-06 10:46:05

标签: php mysql duplicates

我有一个mysql表,看起来像这样:

id_one     id_two
1          2
2          1
3          2
2          3
4          5
5          4

我想删除具有两个重复值的行,无论它们位于哪个列中,所以示例如下所示:

id_one     id_two
1          2
3          2
5          4

总共超过1200万行。关于我应该怎么做的任何想法? 首选Php或mysql查询。

3 个答案:

答案 0 :(得分:1)

DELETE a
FROM table1 a
LEFT JOIN
    (
      select id_one, id_two
      from   Table1
      GROUP BY least(id_one, id_two), greatest(id_one, id_two)
    ) b ON a.id_one = b.id_one AND a.id_two = b.id_two
WHERE b.id_two IS NULL

答案 1 :(得分:0)

我建议采用两步法:

  1. 使id_one始终为较小的值,即如果id_one大于id_two,则交换它们的值 - 考虑这样的事情(取自here):

    UPDATE tablename
    SET id_one = (@temp:=id_one), id_one = id_two, id_two = @temp
    WHERE id_one > id_two
    
  2. 按照here

    所述删除重复项
    DELETE tablename FROM tablename INNER JOIN
        (SELECT min(primary_key) AS min_id, id_one, id_two FROM tablename
         GROUP BY id_one, id_two
         HAVING count(1) > 1) AS d
    ON (d.id_one = tablename.id_one
        AND d.id_two = tablename.id_two
        AND d.min_id <> tablename.primary_key)
    

    (我假设您将拥有一个包含1200万个条目的表的主键。)

  3. 未经测试,请备份您的数据!

答案 2 :(得分:0)

DELETE FROM ztable zt
WHERE zt.id_one > zt.id_two
  AND EXISTS (
    SELECT * 
    FROM ztable tx
    WHERE tx.id_one = zt.id_two
      AND tx.id_two = zt.id_one
    )
    ;

不能在mysql中工作,因为在mysql中你无法引用正在更新或删除的表。 既然你想要制作备份副本,你可以在EXISTS子查询中使用它:

CREATE table safetable AS (SELECT * from ztable);

DELETE FROM ztable zt 
WHERE zt.id_one > zt.id_two 
AND EXISTS (
   SELECT * 
   FROM safetable tx
   WHERE tx.id_one = zt.id_two 
     AND tx.id_two = zt.id_one
   );