CREATE TABLE stu (
id INT,
stuname vARCHAR(45)
);
INSERT INTO stu VALUES(1,“a”);
INSERT INTO stu VALUES(2,‘a’);
INSERT INTO stu VALUES(3,‘b’);
INSERT INTO stu VALUES(4,‘c’);
INSERT INTO stu VALUES(5,‘c’);
INSERT INTO stu VALUES(6,‘d’);
INSERT INTO stu VALUES(7,‘e’);
INSERT INTO stu VALUES(8,‘d’);
SELECT * FROM stu;
DELETE FROM stu
WHERE stuname IN (
SELECT t.stuanme FROM (
SELECT stuname AS stuanme FROM stu GROUP BY stuname HAVING COUNT(*) > 1
) t )
AND id NOT IN (
SELECT f.minid FROM (
SELECT MIN(id) AS minid from stu GROUP BY stuname HAVING COUNT(stuname)>1 ) f);