mysql删除单列重复数据并保留一条

Mysql根据单字段删除重复数据并保留一条

自己刚好有这种需求,又不是很熟悉sql果断去查一下【mysql】mysql删除重复记录并且只保留一条.,自己动手测试一下,顺带说一下Mysql workbench,懒得去找xx破解的话还是可以的,感觉够用了。

CREATE TABLE `ta`.`test2` (
  `idnew_table` VARCHAR(45) NOT NULL,
  `new_tablecol` VARCHAR(45) NULL,
  PRIMARY KEY (`idnew_table`));

select * from test2;

insert into test2(idnew_table,new_tablecol) values('1','c1'),('4','c1'),('2','c1'),('3','c1'),('5','c2'),('41','c2'),('23','c3');

delete from test2 where idnew_table not in (select dt.minno from( select min(idnew_table) as minno  from test2 group by new_tablecol) dt);

#delete from 表名 where 主键 not in ( select dt.minno from (select min(主键) as minno from 表名 group by 重复的字段名) dt);

你可能感兴趣的:(mysql)