創建一張測試表
create table poi ( id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', poi_id bigint(20) NOT NULL COMMENT 'poi_id', PRIMARY KEY (`id`) );
插入測試數據
insert into poi (poi_id) values (10),(10),(10),(20),(20),(30),(40);
查找重復的poi_id
poi_id=10 重復了三條,poi_id=20重復了兩條
select poi_id,count(poi_id) from poi group by poi_id having count(poi_id) > 1;
將重復的數據刪除但是要保留一條
1、查找需要刪除的數據
select * from poi where poi_id in ( select poi_id from poi group by poi_id having count(poi_id) >1 ) and id not in (select min(id) from poi group by poi_id having count(poi_id)>1);
分析:
在重復的poi_id集合中,保留每個重復poi_id中id最小的那條記錄
1、找出重復的POI_ID集合
poi_id in (select poi_id from poi group by poi_id having count(poi_id) >1) 表示找出重復的poi_id
2、在重復的poi_id集合中,按照poi_id分組,保留每個重復的poi_id的最小id
id not in(select min(id) from poi group by poi_id having count(poi_id)>1) 表示在重復的poi_id集合中,按照poi_id分組,保留每個重復的poi_i
2、執行刪除
如果執行將查詢到的數據作為刪除的條件,mysql會提示 “You can't specify target table 'poi' for update in FROM clause”
delete from poi where id in ( select id from poi where poi_id in ( select poi_id from poi group by poi_id having count(poi_id) >1 ) and id not in (select min(id) from poi group by poi_id having count(poi_id)>1) );
遇到這種情況,我們需要將子查詢條件再包一層就可以了
delete from poi where id in ( select id from ( select * from poi where poi_id in ( select poi_id from poi group by poi_id having count(poi_id) >1 ) and id not in (select min(id) from poi group by poi_id having count(poi_id)>1) ) a );