2015-11-07 122 views
-3

我有下表。我需要刪除基於zip列的重複行。mysql從表中刪除重複行

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

謝謝!

回答

1

更簡單的方法是找出你需要保持的....試試這個

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

如果重複的次數是低,你可以重複此命令你需要,否則到時候你shuold使用迭代

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); 

小心嘗試之前進行復印。

+0

#1093 - 你不能指定FROM子句 – john

+0

我有更新的更新目標表「yourTable」答案 – scaisEdge

+0

#1064 - 你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到正確的語法,以便在'AS a where a.id in(從MyTable AS b group中選擇max(b.id)by b.zip ha'at line 1 – john