首页 > 数据库技术 > 详细

mysql删除表中重复数据创建唯一索引。

时间:2019-03-28 11:44:55      阅读:378      评论:0      收藏:0      [点我收藏+]
表结构如下,需要增加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删除表中重复数据创建唯一索引。

原文:https://blog.51cto.com/liuminkun/2370360

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!