测试数据

-- 创建测试表1
CREATE TABLE `testtable1` (
`Id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`UserId` INT(11) DEFAULT NULL,
`UserName` VARCHAR(10) DEFAULT NULL,
`UserType` INT(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `IX_UserId` (`UserId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8; -- 创建测试表2
CREATE TABLE `testtable2` (
`Id` INT(11) NULL AUTO_INCREMENT,
`UserId` INT(11) DEFAULT NULL,
`UserName` VARCHAR(10) DEFAULT NULL,
`UserType` INT(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `IX_UserId` (`UserId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8; -- 插入测试数据1
INSERT INTO testtable1(Id,UserId,UserName,UserType)
VALUES(1,101,'aa',1),(2,102,'bbb',2),(3,103,'ccc',3); -- 插入测试数据2
INSERT INTO testtable2(Id,UserId,UserName,UserType)
VALUES(1,201,'aaa',1),(2,202,'bbb',2),(3,203,'ccc',3),(4,101,'xxxx',5);

可以看到上边的数据中会有userid为重复的数据 userid=101

mysql> show tables;
+---------------+
| Tables_in_dev |
+---------------+
| testtable1 |
| testtable2 |
+---------------+
mysql> select * from testtable1;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 101 | aa | 1 |
| 2 | 102 | bbb | 2 |
| 3 | 103 | ccc | 3 |
+----+--------+----------+----------+
3 rows in set (0.04 sec) mysql> select * from testtable2;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 201 | aaa | 1 |
| 2 | 202 | bbb | 2 |
| 3 | 203 | ccc | 3 |
| 4 | 101 | xxxx | 5 |
+----+--------+----------+----------+
4 rows in set (0.04 sec) ### 当执行以下sql时,会报1062错误,提示有重复的key
mysql> insert into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2;
1062 - Duplicate entry '101' for key 'IX_UserId'

  • 如果想让上边的sql执行成功的话,可以使用IGNORE关键字
mysql> insert ignore into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2;
Query OK, 3 rows affected (0.12 sec)
Records: 4 Duplicates: 1 Warnings:1
mysql> select * from testtable1;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 101 | aa | 1 |
| 2 | 102 | bbb | 2 |
| 3 | 103 | ccc | 3 |
| 11 | 201 | aaa | 1 |
| 12 | 202 | bbb | 2 |
| 13 | 203 | ccc | 3 |
+----+--------+----------+----------+
6 rows in set (0.05 sec)

查询sql,显示testtable2表中的数据插入到了表1中(除了重复key的那条信息)

另外注意到主键id为11,12,13开始的,这个是因为之前insert的sql失败导致的自增主键不连续


导入并覆盖重复数据,REPLACE INTO

上边那个是没有插入重复key的数据

  • 回滚之前testtable1表的数据

    mysql> truncate table testtable1;
    Query OK, 0 rows affected (0.62 sec) mysql> select * from testtable1;
    Empty set
    mysql> -- 插入测试数据1
    INSERT INTO testtable1(Id,UserId,UserName,UserType)
    VALUES(1,101,'aa',1),(2,102,'bbb',2),(3,103,'ccc',3);
    Query OK, 3 rows affected (0.09 sec)
    mysql> replace into testtable1 (userid,username,usertype)
    -> select userid,username,usertype from testtable2;
    Query OK, 5 rows affected (0.10 sec)
    Records: 4 Duplicates: 1 Warnings: 0
    mysql> select * from testtable1;
    +----+--------+----------+----------+
    | Id | UserId | UserName | UserType |
    +----+--------+----------+----------+
    | 2 | 102 | bbb | 2 |
    | 3 | 103 | ccc | 3 |
    | 4 | 201 | aaa | 1 |
    | 5 | 202 | bbb | 2 |
    | 6 | 203 | ccc | 3 |
    | 7 | 101 | xxxx | 5 |
    +----+--------+----------+----------+

    可以看到表1中的101的username被覆盖为表2中的数据,这个是因为replace是现将原来表一中重复的数据删除掉,然后再执行插入新的数据

    导入重复数据,保留未指定的值

    mysql> insert into testtable1 (userid,username,usertype)
    -> select userid,username,usertype from testtable2
    -> on duplicate key update
    -> testtable1.username = testtable2.username;

    以上sql对于重复的数据,只是将username进行了覆盖,其他的值还是表一中的数据

Sql批量插入时如果遇到相同的数据怎么处理的更多相关文章

  1. SQL批量插入表类 SqlBulkInsert

    ado.net已经有了sqlBulkCopy, 但是那个用xml格式,网络传输数据量太大. 自己实现了一个,传输尽量少的字节. 性能没对比过,有需要的自己拿去测试. using System.Data ...

  2. Delphi中SQL批量插入记录

    http://www.cnblogs.com/azhqiang/p/4050331.html 在进行数据库操作时, 我们经常会遇到批量向数据库中写入记录的情况. 在这里我提供3种操作方式:   1.  ...

  3. mybatis oracle mysql 批量插入时的坑爹问题--需谨记

    mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...

  4. insert into select 与select into from -- sql 批量插入

    参考资料:http://www.w3school.com.cn/sql/sql_union.asp   UNION:操作符用于合并两个或多个select语句的结果集.                 ...

  5. 使用SQL批量插入数据到数据库 以及一些SQL函数的语法

    批量插入100条记录 set nocount on declare @i int=1; while @i<=100 begin Insert into Client(id,ClientCode, ...

  6. sql批量插入缓慢

    1.有一个普通的表t_asset,只有2个字段id,ip 没有索引 2.当用insert into t_asset(id,ip) values(?,?),(?,?) 1200多条记录时,发现竟然用了3 ...

  7. Sql批量插入方法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. sql 批量插入

    create  PROCEDURE insertinto as begindeclare @id int;set @id=1;while @id<10begininsert into perso ...

  9. SQL 批量插入数据

    后面进行完善修改. /*批量插入数据*/ 这个比较完善.直接插入数据库表. INSERT INTO `goods_transverter` ( `code`,`es_id`,`barcode`, `n ...

随机推荐

  1. 【机器人M号】题解

    题目 题目描述 3030年,Macsy正在火星部署一批机器人. 第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人. 第2秒,机器人1号造出了第一个机器人--机器人2号. 第3秒,机器人 ...

  2. delphi 10.3 IOS中英文错位

    delphi 每回升级都会遇到各种问题, 在安卓和windows下正常,ios遇到排版问题. 解决办法:将附件文件放至程序目录下. 百度网盘下载附件 链接: https://pan.baidu.com ...

  3. jquery获取元素

       let $lis = $('#sidebar-menu li[to]')//获取sidebar-menu下包含to属性的li

  4. WWW基本概念

    1.Internet 2.Intranet 3.万维网 注:万维网不等同于因特网,它只是因特网的一项服务. 4.TCP/IP 5.HTTP 注:HTTP是运行在应用层的一项服务. 注:服务器在没有用户 ...

  5. 校赛 你的粪坑V2

    原题 今天举办程序设计比赛,2点30分开始,然而你睡到了2点25分,紧张的你将头发梳成大人模样,敷上一层最贵的面膜,穿着滑板鞋,以飞一般的速度奔向计算机学院准备参加程序设计竞赛!冠军是你的! 然而路上 ...

  6. size_t是什么?

    在32位编译器下size_t可看做unsigned int: 在64位编译器下size_t可看做unsigned long long: sizeof返回的数据类型就为size_t. C++之size_ ...

  7. flex几种多列布局

    基本的等分三列布局 .container{ display: flex; width: 500px; height: 200px; } .left{ flex:1; background: red; ...

  8. 克隆虚拟机启动网卡提示错误 Device eth0 does not seem to be present, delaying initialization

    错误原因: 克隆的Linux系统在新的机器上运行,新服务器网卡物理地址已经改变.而/etc/udev/rules.d/70-persistent-net.rules这个文件确定了网卡和MAC地址的信息 ...

  9. SQL优化—nested loop优化

    跑批时间段22:00-23:00,生成AWR报告 分析sql:SQL_ID='5hfw4smzs2pqw' 执行计划: SQL> select *  FROM TABLE(DBMS_XPLAN. ...

  10. 关于问题的四个单词区别: question problem matter issue

    [[ 网上讨论的 problem, question, issue, matter这些名词均含"问题"之意.problem: 指客观上存在的.难以处理或难以理解的问题.questi ...