==============================
©Copyright 蕃薯耀 2023-01-31
蕃薯耀的博客_CSDN博客-蕃薯耀分享,java,js领域博主
一、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):
create table SZ_Building_id
SELECT MIN(id) AS id FROM SZ_Building GROUP BY BLDG_NO
;
- 给临时表的id设置成主键或者创建索引(加快后面的查询速度)
alter table add index ind_xxName(字段名:如ID);
- 删除id不在临时表的数据。
delete from SZ_Building where id not in(
select id from SZ_Building_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