Home > Database > Mysql Tutorial > MySQL中“Insert into xxx on duplicate key update”问题

MySQL中“Insert into xxx on duplicate key update”问题

WBOY
Release: 2016-06-07 17:17:07
Original
1028 people have browsed it

MySQL中ldquo;Insert into xxx on duplicate key updaterdquo;问题。在看代码的过程中碰到了这一用法,不太理解,google了一下

在看代码的过程中碰到了这一用法,不太理解,google了一下。它的意义其实是如果在insert语句末尾制定了“on duplicate key update”语句的话,则当插入行会导致一个unique索引或者primary key中出现重复值,,则执行update中的语句,否则才插入新行。

例如,如果列a被定义为unique,并且值为1,则下列语句有同样的效果,也就是说一旦出入的记录中存在a=1的情况,直接更新c = c + 1,而不执行c = 3的操作。

insert into table(a, b, c) values (1, 2, 3) on duplicate key update c = c + 1;
update table set c = c + 1 where a = 1;

另外值得一提的是,这个语句知识mysql中,而标准sql语句中是没有的。

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template