一个表中重复数据的处理
程序员文章站
2022-05-26 19:37:09
...
查询、删除重复数据的SQL语句 MySQL 查询 columnName 有重复的数据,保留 id 最大的一个。SELECT MAX(id) AS id, columnName, COUNT(columnName ) AS countNumberFROM tableNameGROUP BY columnNameHAVING countNumber 1ORDER BY countNumber DESC;删除 colum
查询 、删除重复数据的SQL语句 MySQL查询 columnName 有重复的数据,保留 id 最大的一个。 SELECT MAX(id) AS id, columnName, COUNT(columnName ) AS countNumber FROM tableName GROUP BY columnName HAVING countNumber> 1 ORDER BY countNumber DESC; 删除 columnName 有重复的数据,保留 id 最小的一个。 DELETE tableName t1 FROM tableName t1, (SELECT min(id) id , columnName FROM tableName GROUP BY columnName HAVING COUNT(1) > 1 ) t2 WHERE t1.columnName = t2.columnName AND t1. id > t2.id;