MySQL:删除表中的的重复复记录,重复的数据只能有一份

步骤一:

创建一张表,并插入数据

create table tt(id int,name varchar(32));
//数据如下:
 mysql> select * from tt;
+------+------+ 
| id   | name |
+------+------+ 
|  100 | aaa  | 
|  100 | aaa  | 
|  200 | bbb  | 
|  200 | bbb  | 
|  200 | bbb  | 
|  300 | ccc  | 
+------+------+

步骤二:

创建一张空表,test,结构和tt一样,

create table test like tt;

 步骤三:

将tt表进行distinct,将数据导入test

insert into test select distinct *from  tt;

步骤四:

删除tt表

drop table tt;

步骤五:

将test表的名字改为tt

rename table test to tt;

或者 

alter table test rename tt;

猜你喜欢

转载自blog.csdn.net/sophie1314/article/details/83692607