您的位置:首页 > 数据库 > MySQL

MYSQL中删除重复记录的方法

2009-06-24 16:15 357 查看
共有三种方法,前两种是利用临时表,下面分别说明:

1、
create temporary table tmp_news select * from news group by nid having count(1) >= 1;

truncate table news;

insert into news select * from tmp_news;

drop table tmp_news;

2、
新建一个临时表
create table tmp_news as select * from news group by nid;
删除原来的表
truncate table youtable;
重命名表
alter table tmp_news rename news;

3、
查找重复的,并且除掉最小的那个。

delete news as a from news as a,
(
select *,min(id) from news group by nid having count(1) > 1
) as b
where a.nid = b.nid and a.id > b.id;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: