mysql去重之實戰總結

1、有主鍵去重

a、單字段重複
 
table  ab
查詢重複記錄(一條)
select aid from ab group by aid having count(*)>1
result:
  查詢重複記錄(所有)
select * from ab t where t.aid in(select aid from ab group by aid having count(*)>1);
result:
   刪除重複記錄保留一條
delete from ab where aid in(
  select aid from ab group by aid having count(*)>1
)and id not in(
  select max(id)as id from ab group by aid having count(*)>1
)

剛開始想到這個sql,結果sql

 因而通過查資料,sql整改
delete from ab where aid in(
    select t.aid from(
        select aid from ab group by aid having count(*)>1
    ) t
)and id not in(
    select t.id from(
        select max(id) as id from ab group by aid having count(*)>1
    ) t
)        
 
 去重成功!
 
  b、多字段重複
table  ab
 查詢重複記錄(一條)
select aid,bid from ab group by aid,bid having count(*)>1
result:
   查詢重複記錄(所有)
select * from ab t where(t.aid,t.bid)in(select aid,bid from ab group by aid,bid having count(*)>1);
result:
 刪除重複記錄保留一條
delete from ab where (aid,bid) in (
	select t.aid,t.bid from (
		select aid,bid from ab group by aid,bid having count(*)>1
	) t
) and id not in (
	select t.id from (
		select max(id) as id from ab group by aid,bid having count(*)>1
	) t
)
去重成功!
 

2、無主鍵去重

單字段、多字段
table ab


  刪除重複記錄保留一條
--建臨時表插入去重數據
create table ab_temp (select * from ab group by aid,bid having count(*)>1);
--刪除重複數據
delete from ab where (aid,bid) in (
	select t.aid,t.bid from (
		select aid,bid from ab group by aid,bid having count(*)>1
	) t 
);
--插入去重數據
insert into ab select * from ab_temp;
--刪除臨時表
drop table ab_temp;
 去重成功!
相關文章
相關標籤/搜索