mysql从表中删除重复的行

时间:2015-11-07 21:04:36

标签: mysql duplicates

我有下表。我需要根据zip列删除重复的行。

id      state       zip
1       CA          112233
2       CA          112233
3       CA          112233
4       CA          113300
.       .           .
.       .           .   
999     FL          345678
1000    FL          234579

谢谢!

2 个答案:

答案 0 :(得分:2)

更简单的方法是确定你需要保留的那些......试试这个

DELETE FROM  `you-table`
WHERE
   id not in (
               select *
                  from(
                       SELECT
                          min(t.id) as keepID
                       from
                           `you-table` as t
                       group by concat(t.state,t.zip)
                      ) as keepTable
              ) 

答案 1 :(得分:1)

如果重复次数较少,您可以在需要时重复此命令,否则您需要使用迭代

delete from yourTable AS a  where a.id in (select max(b.id) from yourTable AS b 
 group by b.zip having count(b.id) >1 );

小心在尝试之前复制一份。