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; |
已經測試,盡請使用
這樣就ok了。
或者這樣操作
| 代碼如下 |
複製代碼 |
ALTER IGNORE TABLE `表名` ADD UNIQUE (`唯一索引欄位名`); |
重複資料刪除的資料,只保留一條。