1. 程式人生 > >記一次mysql去重查詢與刪除重複記錄

記一次mysql去重查詢與刪除重複記錄

查詢:

select *,id,count(*) as count from artist group by id having count>1;

刪除(刪除order_id值大的):

delete from artist where id in( 
SELECT * from 
(select id from artist group by id having count(id) > 1) a)
and order_id not in (
SELECT * from 
(select min(order_id) from artist group by id having count(id )>1) b)

其中:id為重複欄位 order_id為自增欄位