• insert into ... on duplicate key update 与 replace 区别


     on duplicate key update:针对主健与唯一健,当插入值中的主健值与表中的主健值,若相同的主健值,就更新on duplicate key update 后面的指定的字段值,若没有相同主健值,就插入该记录

    mysql> create table tab_test(fd1 int NOT NULL,fd2  varchar(50),primary key(fd1))engine=innodb;     
    Query OK, 0 rows affected (0.22 sec)
    
    mysql> insert into tab_test(fd1,fd2) values(1,"matt");
    Query OK, 1 row affected (0.16 sec)
    
    mysql> insert into tab_test(fd1,fd2) values(2,"toto");
    Query OK, 1 row affected (0.17 sec)
    
    mysql> insert into tab_test values(3,"toto2");         
    Query OK, 1 row affected (0.17 sec)
    
    mysql> insert into tab_test values(3,"toto2") on duplicate key update fd2="test";
    Query OK, 2 rows affected (0.17 sec)
    
    mysql> select * from tab_test;
    +-----+------+
    | fd1 | fd2  |
    +-----+------+
    |   1 | matt |
    |   2 | toto |
    |   3 | test |
    +-----+------+
    3 rows in set (0.00 sec)
    
    mysql> insert into tab_test values(5,"toto2") on duplicate key update fd2="test1";
    Query OK, 1 row affected (0.17 sec)
    
    mysql> select * from tab_test;
    +-----+-------+
    | fd1 | fd2   |
    +-----+-------+
    |   1 | matt  |
    |   2 | toto  |
    |   3 | test  |
    |   5 | toto2 |
    +-----+-------+
    4 rows in set (0.00 sec)

    replace :

    针对主健与唯一健,当插入值中的主健值与表中的主健值,若相同的主健值,删除该记录,再插入值,若没有相同主健值,就插入该记录

    mysql> replace tab_test set fd1=1,fd2="Matt"; 
    Query OK, 2 rows affected (0.17 sec)
    
    mysql> select * from tab_test;
    +-----+-------+
    | fd1 | fd2   |
    +-----+-------+
    |   1 | Matt  |
    |   2 | toto  |
    |   3 | test  |
    |   5 | toto2 |
    +-----+-------+
    4 rows in set (0.00 sec)
    
    mysql> replace tab_test set fd1=1,fd2="xx";
    Query OK, 2 rows affected (0.17 sec)
    
    mysql> select * from tab_test;
    +-----+-------+
    | fd1 | fd2   |
    +-----+-------+
    |   1 | xx    |
    |   2 | toto  |
    |   3 | test  |
    |   5 | toto2 |
    +-----+-------+
    4 rows in set (0.00 sec)
  • 相关阅读:
    初解DLL基本知识
    读《暗时间》笔记
    红队 Cobalt Strike 安全加固
    《天书夜读 —— 从汇编语言到Windows内核编程》3.3 汇编反C语言练习 参考答案
    记录一次从WordPress后台到拿下主机shell的渗透
    今日记 2019.6.19
    解决Termux无法通过metasploit.sh安装Metasploit的问题
    解决ajax中文乱码问题
    jsp开发知识
    第一个Java web项目:员工管理系统
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5637014.html
Copyright © 2020-2023  润新知