MySQL删除冗余数据

MySQL表student

  • 删除冗余数据,并保留一条
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

posted @ 2017-12-07 01:02  希望在田野上  阅读(2904)  评论(0编辑  收藏  举报