在mysql中刪除重復記錄我們有多種方法,下面我介紹利用臨時表與ALTER IGNORE TABLE命令來實現,希望些方法對各位會有所幫助.
mysql刪除字段重復的數據,經過搜索剛開始是這樣做的:
- delete from v_togo
- where tel in (select tel from v_togo group by tel having count(tel) > 1)
- and togoid not in (select min(togoid) from v_togo group by tel having count(tel )>1)
結果mysql報錯:you can't specify target table 'v_togo' for update in from clause
然后我是這樣解決的,分三個步驟:
1、create table tmp as select max(togoid) as toid from v_togo group by tel;先把要處理的字段存儲到臨時表
2、delete from v_togo where togoid not in (select toid from tmp);根據臨時表進行篩選
3、drop table tmp;刪除臨時表
例,我是想做一個去重復操作,比如,如下表:
- id title
- 1 張三
- 2 李四
- 3 張三
- 4 王五
- 5 李四
最終結果是:
- id title
- 1 張三
- 2 李四
- 4 王五
替換方案,代碼如下:
- create table tmp as select min(id) as col1 from blur_article group by title;delete from blur_article where id not in (select col1 from tmp); drop table tmp; --Vevb.com
已經測試,盡請使用這樣就ok了,
或者這樣操作,代碼如下:
ALTER IGNORE TABLE `表名` ADD UNIQUE (`唯一索引字段名`);
刪除重復的數據,只保留一條.
新聞熱點
疑難解答