久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      一起分析MySQL中replace?into與replace區(qū)別

      本篇文章給大家?guī)砹岁P(guān)于mysql的相關(guān)知識(shí),其中主要介紹了MySQL中replace into與replace區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,希望對(duì)大家有幫助。

      一起分析MySQL中replace?into與replace區(qū)別

      推薦學(xué)習(xí):mysql視頻教程

      本篇為拋磚引玉篇,之前沒關(guān)注過replace into 與replace 的區(qū)別。經(jīng)過多個(gè)場(chǎng)景測(cè)試,居然沒找到在插入數(shù)據(jù)的時(shí)候兩者有什么本質(zhì)的區(qū)別?如果了解詳情的伙伴們,請(qǐng)告知留言告知一二,不勝感激?。?!

      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` 這兩個(gè)是聯(lián)合唯一索引,多關(guān)注后續(xù)需要用到!??!

      一起分析MySQL中replace?into與replace區(qū)別

      【需求:】

      • 根據(jù)表格里面, 209133002266賬戶的數(shù)據(jù),重新插入一個(gè)用戶20220302001, 使得新生成的數(shù)據(jù)中strategy_type & status & destroy_at 字段與209133002266用戶的一致。
      • 使用update 一條一條更新也行,但是比較慢。
      • 使用replace into 效果會(huì)高很多,但是深入研究發(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 后面跟表格+需要插入的所有字段名(自動(dòng)遞增字段不用寫) # select 后面選擇的字段,如果根據(jù)查詢結(jié)果取值,則寫字段名;如果是寫死的,則直接寫具體值即可 # 可以理解為,第一部分是插入表格的結(jié)構(gòu),第二部分是你查詢的數(shù)據(jù)結(jié)果

      2.有唯一索引時(shí)—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í)行的時(shí)候,顯示update 12行的數(shù)據(jù)且created at 數(shù)據(jù)更新了,而第一次會(huì)顯示update 6行???

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

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

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

      # 此時(shí)執(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)存在的時(shí)候,replace into 與replace是一樣的
      • 后續(xù)刪除所有20220302001,執(zhí)行1次,2次sql,發(fā)現(xiàn)replace into 與 replace 效果都是一樣的

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

      3.沒有唯一索引時(shí)—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 對(duì)應(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 對(duì)應(yīng)sql:

      一起分析MySQL中replace?into與replace區(qū)別

      一起分析MySQL中replace?into與replace區(qū)別

      • 第二次執(zhí)行replace 對(duì)應(yīng)sql ,因?yàn)闆]有唯一索引限制,結(jié)果原始數(shù)據(jù)居然沒變動(dòng)。又重新生成了新的6條數(shù)據(jù)。
      • 如果后續(xù)還執(zhí)行如上的sql,則數(shù)據(jù)還會(huì)繼續(xù)增加

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

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

      step1:執(zhí)行如下replace into 對(duì)應(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 對(duì)應(yīng)sql:

      一起分析MySQL中replace?into與replace區(qū)別

      一起分析MySQL中replace?into與replace區(qū)別

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

      通過以上分析,沒看出replace into 與replace 具體有啥區(qū)別????有誰知道呢?

      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ū)別

      推薦學(xué)習(xí):mysql視頻教程

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)