mysql新增唯⼀键(mysql5.7)
1. 前提
新增组合唯⼀索引,表中已有⼤量数据,且有很多重复数据
mysql删除重复的数据保留一条解决
1. mysql 5.6 及以下
alter ignore table xy_member_game_count add UNIQUE unique_name (uid,time);
2. mysql 5.7 以上
1.删除表中重复数据,只保留重复中的⼀条
delete from member
where (uid,time) in
(select uid,time from ( select uid,time from member group by uid,time having count()>1) a)
and id not in
( select min(id) from (select min(id) as id from member group by uid,time having count()>1 ) b)
2. 新增索引
alter table member add UNIQUE unique_name (uid,time);
说明:mysql 5.6 弃⽤ alert ignore table 语句,mysql 5.7以上删除 alert ignore table 语句
细节:条件语句中必须多个查询且需要给别名,否则报错You can’t specify target table ‘member’ for update in FROM clause
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论