删除除MySQL数据库表中的一个以外的所有重复值

时间:2017-06-01 05:13:36

标签: mysql

我想删除除MySQL数据库表上的所有重复值之外的所有重复值:enter image description here

我尝试了什么:

DELETE FROM employee WHERE Id NOT IN (
    SELECT MIN(Id) FROM (SELECT * FROM employee) AS employeeSub GROUP BY EmpName
)

1 个答案:

答案 0 :(得分:1)

嗨,你可以试试这个,

Delete from employee where Id not in (select min(Id) from Employee group by EmpName).

或者您可以使用

WITH a as
(
SELECT EmployeeName,ROW_NUMBER() OVER(PARTITION by EmployeeName ORDER BY EmployeeName) 
AS duplicateRecCount
FROM Employee
)
--Now Delete Duplicate Records
DELETE FROM a
WHERE duplicateRecCount > 1