如何删除表格中的所有重复记录?

时间:2013-07-24 21:13:20

标签: mysql sql where sql-delete

我正在寻找一种删除表格中所有重复记录的方法。 我有一个查询,但它不起作用:

delete from lista
where id in
(
    select id
    from lista
    group by tytul_pl
    having count(tytul_pl) > 1
);

服务器响应:

  

错误代码:1093。您无法为更新指定目标表'lista'   FROM子句

此查询:

select id
from lista
group by tytul_pl
having count(tytul_pl) > 1

运作良好。

错误在哪里?

2 个答案:

答案 0 :(得分:1)

这样可以保留一条记录并删除所有其他副本:

DELETE t1 
FROM lista t1
JOIN lista t2 
 ON t1.id > t2.id 
AND t1.tytul_pl = t2.tytul_pl

答案 1 :(得分:0)

我认为你想要的是这个

DELETE lista FROM lista 
LEFT OUTER JOIN (
      SELECT MIN(ID) AS minID 
      FROM lista 
      GROUP BY tytul_pl) AS keepRowTable ON lista.ID = keepRowTable.minID
WHERE keepRowTable.minID IS NULL

你可以try it out here