删除重复条目

时间:2014-01-10 19:53:21

标签: mysql sql

我已成功在phpmyadmin中选择重复查询,如下所示

SELECT *, COUNT(ID)
FROM LINKS
GROUP BY URL
HAVING COUNT( ID ) >1

现在,我需要删除具有重复条目的相同条目,我需要为获取的查询运行什么查询。在删除之前,我想确保我正在运行正确的。提前致谢

2 个答案:

答案 0 :(得分:1)

查看此fiddle。此查询也有效,并保留ID最少的行...

DELETE L1
FROM LINKS L1
    JOIN LINKS L2 ON L1.URL = L2.URL
WHERE L1.ID > L2.ID

答案 1 :(得分:0)

检查:

https://coderwall.com/p/pypbpw?i=4&p=1&q=author%3Amorteza-ipo&t%5B%5D=morteza-ipo

以下是如何删除MySQL中重复行的示例。

DROP FUNCTION IF EXISTS removeDuplicates;
DELIMITER $$
CREATE FUNCTION removeDuplicates(x int)
    RETURNS TINYINT
    BEGIN
        DECLARE x int;
        SET x = x;
        REPEAT

            DELETE FROM `my-queue` WHERE id IN 
            (
                SELECT id FROM 
                (
                    SELECT id, COUNT( * ) AS c,  `to` ,  `msg` 
                    FROM  `my-queue` 
                    GROUP BY  `to` ,  `msg` 
                    HAVING c >1
                ) as tem1
            );

            SET x = x - 1; 
        UNTIL x < 1 END REPEAT;
        RETURN 1;
    END $$
DELIMITER ;

请致电:

select removeDuplicates(100);
祝你好运