1

我相信我的问题是在 SO 上提出的,但我没有找到答案。

有一个mytable带有一列的 mysql 表mycolumn

从表中删除重复项的 mysql 查询是什么?

4

2 回答 2

2

只有一个没有 pk 的列或您可以用来查看它们是否不同的另一列?如果是,这是一个不好的做法。考虑为每条记录插入一个新列(数字)并插入 id,然后您可以尝试以下查询:

delete from table 
where counter > 1 and inner_query.mycolumn = table.mycolumn and inner_query.col_id = table.col_id from 
(select mycolumn, col_id, count (mycolumn) counter  
 from table group by  mycolumn
) inner_query

比,你可以添加一个主键

于 2013-01-07T10:57:22.463 回答
1

只要没有触发器或外键,这是一种解决方法。没有测试,因为我在我的手机上,但应该可以工作。在此之后,可能会在 mycolumn 上创建一个唯一索引以防止重复。

Create table _mytable
Select distinct mycolumn from mytable;

delete from mytable;

Insert into mytable(mycolumn)
Select mycolumn from _mytable;

Drop table _mytable;
于 2013-01-07T10:59:14.417 回答