码迷,mamicode.com
首页 > 其他好文 > 详细

已有数据的表-建联合索引

时间:2019-01-25 17:45:37      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:col   索引   add   style   联合   ons   code   not   pre   

清理重复数据,建立联合唯一索引

1. 查看重复数据

SELECT * FROM holiday_focamobilerel 
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

2. 删除重复数据

- 保留order_id and sub_order_id不为空的

- 如果order_id和sub_order_id都为空,保留id最大的

DELETE FROM holiday_focamobilerel h
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1)
AND h.id NOT IN (select id from holiday_focamobilerel h where h.order_id  is not null  or h.sub_order_id  is not null )
and h.id not in (SELECT MAX(id) FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

 3. 建立联合唯一约束

alter table holiday_focamobilerel add constraint cardnumber_mobile_key unique("cardnumber","mobile")

 

已有数据的表-建联合索引

标签:col   索引   add   style   联合   ons   code   not   pre   

原文地址:https://www.cnblogs.com/weihengblog/p/10320394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!