推荐方法:
用 create ......select......group by ......
先创建临时表tab,新表tab中的数据时从test表中分组查询出来的
create table tab select name ,code from test group by name
拷贝原test的表结构为新表test1。然后删除原表test。
drop table test
此时tab表中的信息已经删除了重复信息(只考虑name字段的重复性)
但是你会发现tab表中没有id,我们需要加上id,所以就将tab表中的数据插入test1中。
用 insert into ......select ......的方法
执行:insert into test1 select null,name,code from tab;
达到去删除重复数据的效果。
最后将表test1改名为test就可以了
alter table test1 rename to test
备注:insert into test1 select null,name,code from tab;
利用mysql主键自增的特性,所以后面的select语句给id赋值为null即可。