关于如何查找、删除mysql数据中的重复数据,我刚好有个机会实践了一下,按照那篇文章进行实践了一下,有问题,但是思路没有问题,下面是一个测试的过程,比较绕,凑合着看吧,我勒个去,我的日志排版太难搞了。
mysql@test.db.test>create table a (id int auto_increment not null primary key,value int);
Query OK, 0 rows affected (0.01 sec) mysql@test.db.test>insert into a(value) values(1),(1),(2),(2),(3),(3),(4),(5);
Query OK, 8 rows affected (0.00 sec)Records: 8 Duplicates: 0 Warnings: 0 mysql@test.db.test>select * from a;
+—-+——-+’ id ‘ value ‘+—-+——-+’ 1 ‘ 1 ‘ ‘ 2 ‘ 1 ‘ ‘ 3 ‘ 2 ‘ ‘ 4 ‘ 2 ‘ ‘ 5 ‘ 3 ‘ ‘ 6 ‘ 3 ‘ ‘ 7 ‘ 4 ‘ ‘ 8 ‘ 5 ‘ +—-+——-+8 rows in set (0.00 sec) mysql@test.db.test>delete from a where value in (select value from a group by value having count(value)>1) and id not in (select min(id) from a group by value having count(value)>1);
ERROR 1093 (HY000): You can’t specify target table ‘a’ for update in FROM clause
mysql@test.db.test>delete from a where value in (select value from (select value from a group by value having count(value)>1)) and id not in ( select min from (select min(id) as min from a group by value having count(value)>1));
ERROR 1248 (42000): Every derived table must have its own alias mysql@test.db.test>delete from a where value in (select value from (select value from a group by value having count(value)>1) as t1) and id not in ( select min from (select min(id) as min from a group by value having count(value)>1) as t2);
Query OK, 3 rows affected (0.00 sec) mysql@test.db.test>select * from a;+—-+——-+’ id ‘ value ‘+—-+——-+’ 1 ‘ 1 ‘ ‘ 3 ‘ 2 ‘ ‘ 5 ‘ 3 ‘ ‘ 7 ‘ 4 ‘ ‘ 8 ‘ 5 ‘ +—-+——-+5 rows in set (0.00 sec)