首先 資料庫中已經存在重複資料 並且資料量很大 之前並沒有設定重複欄位為唯一索引
需求:
修改sql語句在插入時 避免重複插入
網上看了幾個部落格都是設定唯一索引 使用ignore或者replace into 或者on duplicate key update
如果資料存在會觸發更新操作 執行後面語句的update
insert into tb_addrbook(num,name,mobile) value('1001','小李','13112345678') on duplicate key update name= '小李',mobile='13112345678'
on duplicate key update 之後的語句不是條件判斷 條件的判斷會根據你的唯一索引來判斷 觸發update之後會執行 update之後的語句進行更新 如果update之後寫的是條件判斷的話 就會出現只能插入無法更新的操作
如果資料存在就刪除再插入
replace into `student`(`name`, `age`) values('jack', 18);
如果插入時 資料存在則忽略此次插入資料
insert ignore into `student`(`name`, `age`) values('jack', 18);
insert into table (field1, field2, fieldn) select 'field1','field2','fieldn'
from
表名 where
not exists (
select
* from
表名 where
字段= ?
)
查詢資料庫中的重複資料 MySQL資料庫
1 建表語句 drop table if exists t people create table t people id bigint 20 unsigned not null auto increment,people no varchar 18 character set utf8mb4 co...
mysql資料庫去除重複資料
一 最原始的方法 delete from test where id not in select from select min id from test group by name as tmptable 刪除重複,留下id最小的資料 delete from test where id not i...
資料庫併發插入避免重複資料的問題
今天稍微研究了下這個問題從網上整理資料大致得到了如下方案,先粗略記錄下,下班回去再更詳細記錄下。所有的方法都先不考慮效能問題,也不考慮業務,只看邏輯上能否達到去重,因為第一,如果唯一性是必須要保證的,那麼只能先實現了唯一,再去考慮優化 第二,如果能通過調整業務來規避,那我後面說的都是廢話了還有什麼看...