Mysql 删除重复数据保留一条有效数据
================================
©Copyright 蕃薯耀 2023-01-31
https://www.cnblogs.com/fanshuyao/
一、Mysql 删除重复数据,保留一条有效数据
DELETE FROM SZ_Building WHERE id NOT IN ( SELECT t.min_id FROM ( SELECT MIN(id) AS min_id FROM SZ_Building GROUP BY BLDG_NO ) t ) ;
原理:
根据字段对数据进行分组,查询出所有分组的最小ID(即要保留的不重复数据)
将查询出来的数据(所有不重复的数据)存放到临时表中
从原来的表中删除ID不在临时表中的重复数据
当表的数据量达到百万级以上时,删除数据很慢,建议这样处理:
- 先把min(id)存到临时表(SZ_Building_id):
- 给临时表的id设置成主键或者创建索引(加快后面的查询速度)
- 删除id不在临时表的数据。
二、Mysql 删除重复数据(多个字段分组)
DELETE FROM SZ_Water_Level WHERE id NOT IN ( SELECT t.min_id FROM ( SELECT MIN(id) AS min_id FROM SZ_Water_Level GROUP BY CZBM,SJ,SW ) t ) ;
三、Mysql 查询出可以删除的重复数据
SELECT * FROM SZ_Building WHERE id NOT IN ( SELECT t.min_id FROM ( SELECT MIN(id) AS min_id FROM SZ_Building GROUP BY BLDG_NO ) t ) ;
或
SELECT * FROM SZ_Building WHERE BLDG_NO IN ( SELECT BLDG_NO FROM SZ_Building GROUP BY BLDG_NO HAVING COUNT(1)>1 ) AND id NOT IN ( SELECT MIN(id) FROM SZ_Building GROUP BY BLDG_NO HAVING COUNT(1)>1 ) ;
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
©Copyright 蕃薯耀 2023-01-31
https://www.cnblogs.com/fanshuyao/
今天越懒,明天要做的事越多。