mysql多字段去重保留一條記錄並添加聯合唯一索引


-- 刪除重復的記錄(會保留一條),然后建立唯一索引,高效而且人性化。(注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);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM