1. 程式人生 > >mysql刪除表中重復數據創建唯一索引。

mysql刪除表中重復數據創建唯一索引。

delete 刪除 delet ima nod not null rem group rom

表結構如下,需要增加xx,yy復合唯一索引。
create table table_a (
id int(11) NOT NULL AUTO_INCREMENT,
xx int(11) NOT NULL,
yy int(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

保留最小id
delete a.* from table_a as a,( select min(id) id , xx,yy from table_a group by xx,yy having count(1)>1
) as b where a.yy=b.yy and a.xx=b.xx and a.id > b.id;

保留最大id
delete a.* from table_a as a,( select max(id) id , xx,yy from table_a group by xx,yy having count(1)>1
) as b where a.yy=b.yy and a.xx=b.xx and a.id < b.id;

mysql刪除表中重復數據創建唯一索引。