时间:2021-07-01 10:21:17 帮助过:155人阅读
查询、删除重复数据的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;