MySql去重

來源:互聯網
上載者:User

標籤:

常用的有兩種方法:

1、單語句法:

1)delete b from sys_test b ,(select *,max(uuid) from sys_test group by `name` having count(`name`) > 1) as d where b.uuid>d.uuid and b.name = d.name

delete b from sys_test b這一句是設定別名,delete語句一般直接接from,當需要設定from表別名是,需要把別名放在from之前,表示刪除時是對該別名中內容進行刪除。

from中不同的表用,分割,可以有子查詢出來的表,後面再接where即可。

2)delete form user where  id not in (select Id from (select Max(ID) as Id,姓名,社會安全號碼 from User group by 姓名,社會安全號碼) as t );

該方法效率略低

2、暫存資料表法

1)、找出所有重複的資料,並把重複資料複製一條到暫存資料表

  select * into #temp1 from user group by `name` having count(`name`) > 1

2)、刪除原表中的所有重複的資料

      delete from user where name in (select `name` from user group by `name` having count(`name`) > 1);

3)、將暫存資料表中的資料在插入回user表

  insert into user  select * from #temp1;

4)、刪除暫存資料表

      drop #temp1;

MySql去重

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.