• 技术文章 >数据库 >mysql教程

    一起分析MySQL中replace into与replace区别

    WBOYWBOY2022-08-22 19:55:05转载473
    本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了MySQL中replace into与replace区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,希望对大家有帮助。

    php入门到就业线上直播课:进入学习

    推荐学习:mysql视频教程

    本篇为抛砖引玉篇,之前没关注过replace into 与replace 的区别。经过多个场景测试,居然没找到在插入数据的时候两者有什么本质的区别?如果了解详情的伙伴们,请告知留言告知一二,不胜感激!!!

    0.故事的背景

    【表格结构】

    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` 这两个是联合唯一索引,多关注后续需要用到!!!

    【需求:】

    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 后面选择的字段,如果根据查询结果取值,则写字段名;如果是写死的,则直接写具体值即可
    # 可以理解为,第一部分是插入表格的结构,第二部分是你查询的数据结果

    2.有唯一索引时—replace into & 与replace 效果

    step1: 第一次执行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;

    【执行完之后,查询结果如下:】

    step2: 第二次执行sql情况

    为什么第二次执行的时候,显示update 12行的数据且created at 数据更新了,而第一次会显示update 6行???

    1.因为在执行sql的时候,replace into 其实分了两个步骤执行。第一步是将查询到数据转化为新的数据。第二步, 新的数据如果表中已经有相同的内容,则删除掉。如果没有相同的内容,则直接插入新的数据。

    2.因如上第一次执行的时候,已经生成一次新数据了,第二次会先删除,再把最新的数据插入进去,最终才显示update 12 行

    step3: 第三次执行sql情况

    # 此时执行的是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;

    【总结:】当有唯一索引限制的时候,如果新增的数据会受限于唯一索引,则数据只会插入一次,如果已经存在则会先删除再插入。此时replace into 与replace 效果一样。

    3.没有唯一索引时—replace into 与 replace

    我们将strategy_type & user_name 联合唯一索引删除,且删除20220302001用户所有数据。最终表格结构如下:

    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函数的具体情况

    step1:执行如下replace 对应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:再次执行replace 对应sql:

    2).replace into 函数的具体情况

    执行之前,先清理数据,将所有20220302001的数据都删除掉

    step1:执行如下replace into 对应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;

    step2:再次执行replace into 对应sql:

    最终发现,没有唯一索引的时候,replace into 与replace 居然一摸一样的效果,都是继续增加数据。

    通过以上分析,没看出replace into 与replace 具体有啥区别????有谁知道呢?

    4.replace的用法

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

    推荐学习:mysql视频教程

    以上就是一起分析MySQL中replace into与replace区别的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:脚本之家,如有侵犯,请联系admin@php.cn删除

    千万级数据并发解决方案(理论+实战):点击学习

    Mysql单表千万级数据量的查询优化与性能分析

    Mysql主从原理及其在高并发系统中的应用

    专题推荐:mysql
    上一篇:MySQL中的日期时间类型与格式化方式总结 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• MySQL中关于超键和主键及候选键的区别分析• 怎么解决1045无法登录mysql服务器• 归纳总结之MySQL数据库子查询语法规则• MySQL学习之日期函数的用法详解• 归纳整理MySQL存储过程参数的用法及说明
    1/1

    PHP中文网