mysql> create table test1(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mys
mysql> create table test2(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | aaa | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
+-----+------+------+
3 rows in set (0.00 sec)
mysql> select * from test2;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 201 | aaa | 1 |
| 202 | bbb | 2 |
| 203 | ccc | 3 |
| 101 | xxx | 5 |
+-----+------+------+
4 rows in set (0.00 sec)
发现重复的是更新操作。在原有记录基础上,更新指定字段内容,其它字段内容保留。例如我只想插入test2表的id,name字段,但是要保留test1表的type字段:
mysql> insert into test1(id,name,type)(select id,name,type from test2) on DUPLICATE KEY UPDATE test1.name=test2.name;
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from test1;
+-----+------+------+
| id | name | type |
+-----+------+------+
| 101 | xxx | 1 |
| 102 | bbb | 2 |
| 103 | ccc | 3 |
| 203 | ccc | 3 |
| 202 | bbb | 2 |
| 201 | aaa | 1 |
+-----+------+------+
6 rows in set (0.00 sec)
相关推荐
[mysql]mysql通过on duplicate key update实现批量插入或更新(csdn)————程
mysql “ON DUPLICATE KEY UPDATE” 语法如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE;如果不会导致唯一值列...
代码如下: insert into table(a, b, c) values (1, 2, 3) on duplicate key update c = c + 1;1 update table set c = c + 1 where a = 1; 另外值得一提的是,这个语句知识mysql中,而标准sql语句中是没有的。 ...
本文介绍一下关于mysql中INSERT INTO… ON DUPLICATE KEY UPDATE用法
今天听同事介绍oracle到mysql的数据migration,他用了Insert into ..... on duplicate key update ...,我当时就想怎么不用Replace呢,于是回来就仔细查了下,它们果然还是有区别的
mysql数据库Insert语句后加ONDUPLICATE KEY UPDATE,保证唯性如果在INSERT语句末尾指定了ON DUPLICATE KEY UP
先看语法INSERT INTO table(id,a,……) values(1,‘xh’,……) ON DUPLICATE KEY Update a=‘value’,……; 举个例子 1,先创建表结构如下 DROP TABLE IF EXISTS `t_ware_duplicate`; CREATE TABLE `t_ware_...
本文实例讲述了mysql 中 replace into 与 insert into on duplicate key update 的用法和不同点。分享给大家供大家参考,具体如下: replace into和insert into on duplicate key update都是为了解决我们平时的一个...
`INSERT INTO tbl_name (col_name1, col_name2, ...) VALUES (col_value1, col_value2, ...), (col_value1, col_value2, ...) ON DUPLICATE KEY UPDATE userName = VALUES(userName), userID = VALUES(userID);...
在对看看的后台进行排序的时候,遇到了一个像这样的需求,在电影表中有ID(主键自增)和orderby(排序字段) ,假设有十条数据id分别从1-10之间,对应的...下面来看看Mysql的REPLACE和INSERT … ON DUPLICATE KEY UPDATE REP
在 MySQL 中,我们可以使用 ON DUPLICATE KEY UPDATE 关键字来实现数据不存在则插入,存在则更新的操作。判断数据重复的逻辑依然是主键冲突或者唯一键冲突。 例如,以下是使用 ON DUPLICATE KEY UPDATE 关键字的...
本篇文章是对mysql "ON DUPLICATE KEY UPDATE"语法进行了详细的分析介绍,需要的朋友参考下
最好用的硬盘文件查重工具,文件整理,删除重复文件,释放硬盘空间
* The Delphi 7 documentation PDF files are available on the update CD.========================================================UPDATING LOCALIZED VERSIONS OF DELPHI 7* This update can be applied only ...
INSERT语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,…)] VALUES ({expr | DEFAULT},…),(…),… [ ON DUPLICATE KEY UPDATE col_name=expr, … ] 或: INSERT [LOW_...
交换表的行 TableID.moveRow(2,1) 替换CSS document.all.csss.href = "a.css"; 并排显示 display:inline 隐藏焦点 hidefocus=true 根据宽度换行 style="word-break:break-all" 自动刷新 ...