-- 刪除重復的記錄(會保留一條),然后建立唯一索引,高效而且人性化。(注mysql5.732版本以上語法無效)
alter ignore table t_aa add unique index index_name (aa,bb);
-- 查詢大於一條的重復記錄
SELECT * from t_event WHERE id not in( SELECT min_id from (SELECT MIN(id) as min_id from t_event GROUP BY time,type1,type2,forklift_box_id) t )
-- 刪除重復並保留id值最小的記錄
DELETE from t_event WHERE id not in( SELECT min_id from (SELECT MIN(id) as min_id from t_event GROUP BY time,type1,type2,forklift_box_id) t )
-- 添加聯合唯一索引(索引名可以不寫)
alter table fms_forklift_device_run_record add unique index index_name (fms_forklift_id,start_time); ALTER TABLE fms_forklift_device_run_record ADD UNIQUE INDEX (fms_forklift_id,start_time);
