select aid from ab group by aid having count(*)>1
select * from ab t where t.aid in(select aid from ab group by aid having count(*)>1);
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
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 )
select aid,bid from ab group by aid,bid having count(*)>1
select * from ab t where(t.aid,t.bid)in(select aid,bid 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 ) and id not in ( select t.id from ( select max(id) as id from ab group by aid,bid having count(*)>1 ) t )
--建臨時表插入去重數據 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;