V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
eabet
V2EX  ›  MySQL

MYSQL 插入时判断重复就不插入,不影响原记录

  •  
  •   eabet · 2016-12-20 20:49:54 +08:00 · 3864 次点击
    这是一个创建于 2655 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 REPLACE INTO 的话是更新,有没有办法不影响原记录呢?
    11 条回复    2016-12-21 23:01:56 +08:00
    julyclyde
        1
    julyclyde  
       2016-12-20 21:23:47 +08:00
    insert ignore?
    billlee
        2
    billlee  
       2016-12-20 21:49:09 +08:00
    那你为什么用 REPLACE 干嘛,用 INSERT
    li24361
        3
    li24361  
       2016-12-20 21:59:55 +08:00
    ON DUPLICATE KEY UPDATE
    inaho
        4
    inaho  
       2016-12-20 22:01:58 +08:00
    楼上+1 , 加个唯一索引
    TaMud
        5
    TaMud  
       2016-12-20 22:17:05 +08:00
    1 、唯一索引
    2 、 WHERE NOT EXISTS
    darrenfang
        6
    darrenfang  
       2016-12-21 07:47:21 +08:00 via iPhone
    insert ignore
    wwulfric
        7
    wwulfric  
       2016-12-21 10:39:36 +08:00
    唯一索引会报错啊
    Zuckonit
        8
    Zuckonit  
       2016-12-21 11:02:31 +08:00
    1. mysql
    2. unique index
    3. ON DUPLICATE KEY UPDATE / INSERT IGNORE
    anthow
        9
    anthow  
       2016-12-21 11:48:47 +08:00
    merge into ... when not matched then insert...
    ihuotui
        10
    ihuotui  
       2016-12-21 12:03:52 +08:00
    捕获唯一索引的错误,然后变为更新操作啊。
    rekulas
        11
    rekulas  
       2016-12-21 23:01:56 +08:00
    insert into table1(a,b,c) select v1,v2,v3 from dual where not exists (select * from table1 where a='a')
    这样也可以无重复插入
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4464 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:03 · PVG 18:03 · LAX 03:03 · JFK 06:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.