久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

149次閱讀
沒有評論

共計 3513 個字符,預(yù)計需要花費(fèi) 9 分鐘才能閱讀完成。

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

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ù)需要用到!!!

【需求:】

根據(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;

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

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

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

1. 因?yàn)樵趫?zhí)行 sql 的時候,replace into 其實(shí)分了兩個步驟執(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;

最終查看到的情況與第二次執(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;

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

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

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

第二次執(zhí)行 replace 對應(yīng) sql,因?yàn)闆]有唯一索引限制,結(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 的用法

單獨(dú) 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)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-13發(fā)表,共計3513字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 保定市| 白城市| 南昌县| 涪陵区| 黄梅县| 咸丰县| 红安县| 桦甸市| 鸡西市| 清水县| 绥阳县| 辉县市| 通州区| 九寨沟县| 仙游县| 达尔| 富裕县| 疏附县| 齐河县| 沾益县| 菏泽市| 吉安县| 马山县| 德江县| 阜康市| 镇沅| 如皋市| 微山县| 德兴市| 镇赉县| 唐山市| 新营市| 鸡泽县| 周至县| 巧家县| 延寿县| 徐水县| 三门峡市| 阜阳市| 谢通门县| 南川市|