mysql刪除欄位重複的資料sql語句

來源:互聯網
上載者:User

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 (`唯一索引欄位名`);

重複資料刪除的資料,只保留一條。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.