溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL中replace?into與replace區(qū)別是什么

發(fā)布時間:2022-08-24 11:37:17 來源:億速云 閱讀:157 作者:iii 欄目:MySQL數(shù)據(jù)庫

本篇內(nèi)容介紹了“MySQL中replace into與replace區(qū)別是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

MySQL中replace?into與replace區(qū)別是什么

0.故事的背景

【表格結(jié)構(gòu)】

CREATE TABLE `xtp_algo_white_list` (
  `strategy_type` int DEFAULT NULL,
  `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `status` int DEFAULT NULL,
  `destroy_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `xtp_algo_white_list_UN` (`strategy_type`,`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin


# `strategy_type`,`user_name` 這兩個是聯(lián)合唯一索引,多關(guān)注后續(xù)需要用到?。?!

MySQL中replace?into與replace區(qū)別是什么

【需求:】

  • 根據(jù)表格里面, 209133002266賬戶的數(shù)據(jù),重新插入一個用戶20220302001, 使得新生成的數(shù)據(jù)中strategy_type & status & destroy_at 字段與209133002266用戶的一致。

  • 使用update 一條一條更新也行,但是比較慢。

  • 使用replace into 效果會高很多,但是深入研究發(fā)現(xiàn)也有一些坑的地方

1.replace into 的使用方法

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

# replace into 后面跟表格+需要插入的所有字段名(自動遞增字段不用寫)
# select 后面選擇的字段,如果根據(jù)查詢結(jié)果取值,則寫字段名;如果是寫死的,則直接寫具體值即可
# 可以理解為,第一部分是插入表格的結(jié)構(gòu),第二部分是你查詢的數(shù)據(jù)結(jié)果

2.有唯一索引時—replace into & 與replace 效果

step1: 第一次執(zhí)行sql情況

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

MySQL中replace?into與replace區(qū)別是什么

【執(zhí)行完之后,查詢結(jié)果如下:】

MySQL中replace?into與replace區(qū)別是什么

step2: 第二次執(zhí)行sql情況

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

為什么第二次執(zhí)行的時候,顯示update 12行的數(shù)據(jù)且created at 數(shù)據(jù)更新了,而第一次會顯示update 6行???

1.因為在執(zhí)行sql的時候,replace into 其實分了兩個步驟執(zhí)行。第一步是將查詢到數(shù)據(jù)轉(zhuǎn)化為新的數(shù)據(jù)。第二步, 新的數(shù)據(jù)如果表中已經(jīng)有相同的內(nèi)容,則刪除掉。如果沒有相同的內(nèi)容,則直接插入新的數(shù)據(jù)。

2.因如上第一次執(zhí)行的時候,已經(jīng)生成一次新數(shù)據(jù)了,第二次會先刪除,再把最新的數(shù)據(jù)插入進(jìn)去,最終才顯示update 12 行

step3: 第三次執(zhí)行sql情況

# 此時執(zhí)行的是replace 

replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

  • 最終查看到的情況與第二次執(zhí)行的sql一樣。

  • 當(dāng)新數(shù)據(jù)已經(jīng)存在的時候,replace into 與replace是一樣的

  • 后續(xù)刪除所有20220302001,執(zhí)行1次,2次sql,發(fā)現(xiàn)replace into 與 replace 效果都是一樣的

【總結(jié):】當(dāng)有唯一索引限制的時候,如果新增的數(shù)據(jù)會受限于唯一索引,則數(shù)據(jù)只會插入一次,如果已經(jīng)存在則會先刪除再插入。此時replace into 與replace 效果一樣。

3.沒有唯一索引時—replace into 與 replace

我們將strategy_type & user_name 聯(lián)合唯一索引刪除,且刪除20220302001用戶所有數(shù)據(jù)。最終表格結(jié)構(gòu)如下:

CREATE TABLE `xtp_algo_white_list` (
  `strategy_type` int DEFAULT NULL,
  `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `status` int DEFAULT NULL,
  `destroy_at` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

1).replace函數(shù)的具體情況

step1:執(zhí)行如下replace 對應(yīng)sql:

replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

step2:再次執(zhí)行replace 對應(yīng)sql:

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

  • 第二次執(zhí)行replace 對應(yīng)sql ,因為沒有唯一索引限制,結(jié)果原始數(shù)據(jù)居然沒變動。又重新生成了新的6條數(shù)據(jù)。

  • 如果后續(xù)還執(zhí)行如上的sql,則數(shù)據(jù)還會繼續(xù)增加

2).replace into 函數(shù)的具體情況

執(zhí)行之前,先清理數(shù)據(jù),將所有20220302001的數(shù)據(jù)都刪除掉

step1:執(zhí)行如下replace into 對應(yīng)sql:

replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`)
select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

step2:再次執(zhí)行replace into 對應(yīng)sql:

MySQL中replace?into與replace區(qū)別是什么

MySQL中replace?into與replace區(qū)別是什么

最終發(fā)現(xiàn),沒有唯一索引的時候,replace into 與replace 居然一摸一樣的效果,都是繼續(xù)增加數(shù)據(jù)。

4.replace的用法

  • 單獨replace的作用是替換字段中某數(shù)值的顯示效果??梢詳?shù)值中的部分替換、也可以全部替換。

  • 如下表格,將user_name的字段,20220302改為"A_20220303"顯示,并且新字段叫做new_name顯示

MySQL中replace?into與replace區(qū)別是什么

select *, replace(user_name,20220302,'A_20220303') as "new_name" from xtp_algo_white_list where user_name = 20220302001;

MySQL中replace?into與replace區(qū)別是什么

“MySQL中replace into與replace區(qū)別是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI