多个 UNIQUE 字段的 ON DUPLICATE KEY UPDATE 的 MySQL 行为

MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields(多个 UNIQUE 字段的 ON DUPLICATE KEY UPDATE 的 MySQL 行为)
本文介绍了多个 UNIQUE 字段的 ON DUPLICATE KEY UPDATE 的 MySQL 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 MySQL 4.1.0 开始,可以添加 ON DUPLICATE KEY UPDATE 语句来指定插入值时的行为(使用 INSERTSETVALUES) 已经在目标表中PRIMARY KEY 或一些 UNIQUE 字段.如果 PRIMARY KEY 或某些 UNIQUE 字段的值已在表中,则 INSERT 将替换为 UPDATE.

From MySQL 4.1.0 onwards, it is possible to add ON DUPLICATE KEY UPDATE statement to specify behavior when values inserted (with INSERT or SET or VALUES) are already in destination table w.r.t. PRIMARY KEY or some UNIQUE field. If value for PRIMARY KEY or some UNIQUE field are already in table, INSERT is replaced by an UPDATE.

  • ON DUPLICATE KEY UPDATE 在有多个的情况下如何表现UNIQUE 我的表中的字段?

  • How does ON DUPLICATE KEY UPDATE behave in case there are multiple UNIQUE fields in my table ?

如果任一 UNIQUE 字段匹配,我可以只更新一次吗?

Can I have one update only, if either UNIQUE field is matched ?

只有当两个 UNIQUE 字段同时匹配时,我才能更新吗?

Can I have an update only if both UNIQUE fields are matched simultaneously ?

推荐答案

考虑

INSERT INTO table (a,b,c) VALUES (1,2,3)
    -> ON DUPLICATE KEY UPDATE c=c+1;

如果 a 和 b 是 UNIQUE 字段,则 UPDATE 发生在 a = 1 OR b = 2 上.此外,当两个或多个条目满足条件 a = 1 OR b = 2 时,仅更新一次.

If a and b are UNIQUE fields, UPDATE occurs on a = 1 OR b = 2. Also when condition a = 1 OR b = 2 is met by two or more entries, update is done only once.

Ex here table table with Id 和 Name UNIQUE 字段

Ex here table table with Id and Name UNIQUE fields

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6

如果查询是

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7);

然后我们得到

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6
1      C        7

这违反了 Id 和 Name 的唯一性.现在有了

which violates uniqueness of Id and Name. Now with

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7)
ON DUPLICATE KEY UPDATE Value = 7;

我们得到

Id     Name     Value 
1      P        7 
2      C        7 
3      D        29 
4      A        6

多个键的行为如下

UPDATE.这里,UPDATE 是在 Id = 1 OR Name = C 上执行的.相当于

UPDATE in ON DUPLICATE KEY UPDATE is performed if one of the UNIQUE field equals the value to be inserted. Here, UPDATE is performed on Id = 1 OR Name = C. It is equivalent to

UPDATE table 
SET Value = 7
WHERE Id = 1 OR Name = C;

如果我只想要一个更新,对于任一键怎么办

可以使用带有LIMIT关键字的UPDATE语句

Can use UPDATE statement with LIMIT keyword

UPDATE table 
SET Value = 7
WHERE Id = 1 OR Name = C
LIMIT 1;

这将给

Id     Name     Value 
1      P        7 
2      C        3 
3      D        29 
4      A        6

如果我只想在两个键的值都匹配的情况下进行一次更新怎么办

一种解决方案是 ALTER TABLE 并使 PRIMARY KEY(或唯一性)在这两个字段上都起作用.

One solution is to ALTER TABLE and make the PRIMARY KEY (or uniqueness) work on both fields.

ALTER TABLE table 
DROP PRIMARY KEY,
ADD PRIMARY KEY (Id, Name);

现在开始

INSERT INTO table (Id, Name, Value)
VALUES (1, C, 7)
ON DUPLICATE KEY UPDATE Value = 7;

我们得到

Id     Name     Value 
1      P        2 
2      C        3 
3      D        29 
4      A        6
1      C        7

因为没有找到重复项(在两个键上).

since no duplicate (on both keys) is found.

这篇关于多个 UNIQUE 字段的 ON DUPLICATE KEY UPDATE 的 MySQL 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)