0

我需要从表中删除存在重复link列的行。我正在尝试运行:

delete from resultitem 
       where id in (select r.id 
                    from resultitem r 
                    group by r.link
                    having count(r.id) >1);

但得到一个错误:

ERROR 1093 (HY000): You can't specify target table 'resultitem' for update in FROM clause

这是否可以在没有临时表的情况下通过 MySQL 中的子查询删除行?请指教。

4

4 回答 4

4

这应该删除除最低idper之外的所有内容link

delete  ri1
from    resultitem as ri1
inner join
        resultitem as ri2
on      ri1.link = ri2.link
        and ri1.id > ri2.id

SQL Fiddle 上的实时示例。

要删除所有重复链接,不留下任何重复链接,请删除and ri1.id > ri2.id.

于 2012-09-12T08:40:04.280 回答
1
DELETE resultitem
FROM resultitem
LEFT OUTER JOIN (
   SELECT MIN(id) as RowId, link
   FROM resultitem
   GROUP BY link
) as KeepRows ON
   resultitem.id = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL
于 2012-09-12T08:46:54.113 回答
0

试试这个...

delete r from resultitem r
INNER JOIN (
       (select id 
                    from resultitem rr 
                    group by rr.link
                    having count(rr.id) >1) rr
ON r.id = rr.id;
于 2012-09-12T08:41:00.033 回答
0
delete from resultitem 
       where id not in (select r.id  
                  from (select r1.id, unique(r1.link) from resultitem r1) as r );
于 2012-09-12T08:47:15.020 回答