Home  >  Article  >  Database  >  mysql两个小知识点”INSERT IGNORE”,”ON DUPLICATE KEY UPDAT

mysql两个小知识点”INSERT IGNORE”,”ON DUPLICATE KEY UPDAT

WBOY
WBOYOriginal
2016-06-07 16:41:373451browse

1.INSERT IGNORE INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。 eg: insert ignore into table(name)? select? name from table2 2.ON DUPLICATE

1.INSERT IGNORE

INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。
eg:
insert ignore into table(name)? select? name from table2

2.ON DUPLICATE KEY UPDATE

如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE。例如,如果列a被定义为UNIQUE,并且包含值1,则以下两个语句具有相同的效果:
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;
如果行作为新记录被插入,则受影响行的值为1;如果原有的记录被更新,则受影响行的值为2。

Statement:
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