# 删除重复数据只保留一条 DELETE from `user` where (name, pwd) in ( SELECT name, pwd from ( select name, pwd from `user` groupby name, pwd havingcount(*) >1 ) t ) and id notin ( SELECT a.id from ( selectmin(id) as id from `user` groupby name, pwd havingcount(*) >1 ) a );
执行此段代码之后, name , pwd 同时相等的数据被删除,也就是行号为 4, 5 的两行,但这里选择保留 id 最小的一行,也就是第 4 行,执行之后的结果为:
DELETE from $tableName$ where ($fields$) in ( SELECT $fields$ from ( select $fields$ from $tableName$ groupby $fields$ havingcount(*) >1 ) t ) and id notin ( SELECT a.id from ( selectmin(id) as id from $tableName$ groupby $fields$ havingcount(*) >1 ) a );