0

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

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

服务器响应:

错误代码:1093。您不能在 FROM 子句中指定目标表 'lista' 进行更新

这个查询:

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

运作良好。

错误在哪里?

4

2 回答 2

1

这将有助于保留一个记录并删除所有其他重复项:

DELETE t1 
FROM lista t1
JOIN lista t2 
 ON t1.id > t2.id 
AND t1.tytul_pl = t2.tytul_pl
于 2013-07-24T21:32:03.993 回答
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

你可以在这里试试

于 2013-07-24T21:30:51.030 回答