我正在使用以下查询,这是我从另一个 stackoverflow 问题中看到的,但出现错误。
delete from mytable
where myid not in (
select max(myid)
from mytable
group by myid2)
错误:
#1093 - Table 'mytable' is specified twice, both as a target for 'DELETE' and as a separate source for data
编辑2:
我也尝试过这个查询:
delete from mytable
where myid in (
SELECT
myid, COUNT(*)
FROM
mytable
GROUP BY
myid2
HAVING
COUNT(*) > 1)
并收到此错误:
#1241 - Operand should contain 1 column(s)
最佳答案
在 MySQL 中,您需要使用 JOIN
来实现此目的。我想你的意思是这样的:
delete t
from mytable t left join
(select max(myid) as myid
from mytable
group by myid2
) tt
on t.myid = tt.myid
where tt.myid is null;
Where ?
是您真正想要分组的依据。您的版本不会删除任何内容,因为 group by
和 max()
使用同一列。
关于mysql - 如何删除MySQL表中的重复行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313371/