MySQL删除冗余数据

MySQL表student

MySQL删除冗余数据_第1张图片

  • 删除冗余数据,并保留一条
DELETE FROM student WHERE id NOT IN (SELECT MIN(s.id) FROM (SELECT * FROM student) s GROUP BY s.name,s.code,s.course,s.score,s.gender);
DELETE FROM student WHERE id NOT IN (SELECT * FROM (SELECT MIN(s.id) FROM  student s GROUP BY s.name,s.code,s.course,s.score,s.gender)st );

MIN()函数换成MAX()也一样;
如果是DELETE FROM student WHERE id NOT IN (SELECT MIN(s.id) FROM student s GROUP BY s.name,s.code,s.course,s.score,s.gender);,则MySQL报错
You can't specify target table 'student' for update in FROM clause

转载于:https://www.cnblogs.com/isxt/p/7995729.html

你可能感兴趣的:(MySQL删除冗余数据)