首页 > 数据库技术 > 详细

MySql去重

时间:2015-05-05 14:05:46      阅读:200      评论:0      收藏:0      [点我收藏+]

常用的有两种方法:

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去重

原文:http://www.cnblogs.com/guangshan/p/4478859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!