Mysql 使用sql删除同表中重复数据并加唯一索引

同一张表中,假设以两个字段做唯一业务,这两个字段分别为key1,key2,

则以这两个字段为唯一

DELETE tablename FROM
    tablename ,
    (
        SELECT
            min(id) id,
           key1, key2
        FROM
            tablename 
        GROUP BY
            
            key1, key2
        HAVING
            count(*) > 1
    ) t2
		WHERE
 tablename .key1=t2.key1 
AND tablename .key2=t2.key2
AND tablename .id > t2.id;

 增加唯一索引

ALTER TABLE t_procurement_order_instock add unique index uniq_t_procurement_order_instock(`t_procurement_order_instock_id`);  

ALTER TABLE tablename DROP INDEX ix_instock_date_warehouse_g_f_lu_standard_item, ADD INDEX idx_f_lu_standard_item_warehouse_g_item_status_instock_date(`f_lu_standard_item`,`warehouse_g`,`item_status`,`instock_date`);
ALTER TABLE tablename DROP INDEX IND_PAY_ORDER_1;                                                                     ALTER IGNORE TABLE PAY_ORDER ADD UNIQUE INDEX IND_PAY_ORDER_1(ORDER_ID);               将ORDER_ID设为唯一索引(IND_PAY_ORDER_1)删除重复数据

  

posted @ 2019-10-14 13:18  stevenlii  阅读(1043)  评论(0编辑  收藏  举报