1、如果没有主键,则新增自增主键:
-- 改变表结构,增加名为id的列 ALTER TABLE emp add id int first; -- 将id字段设置为主键自增; ALTER table emp MODIFY id int PRIMARY key auto_increment;
2、根据name分组取最小id
SELECT t.id FROM ( SELECT MIN(id) AS id FROM emp GROUP BY name ) t)
3、利用id字段not in删除,最终sql:
DELETE FROM emp WHERE id NOT IN ( SELECT t.id FROM ( SELECT MIN(id) AS id FROM emp GROUP BY name ) t)
这就达成目的了。
标签:GROUP,name,重复,Mysql,主键,emp,去除,id,SELECT From: https://www.cnblogs.com/yang5726685/p/17459721.html