为什么在我的`INSERT ... ON DUPLICATE KEY UPDATE`中有 2 行受到影响?

Why are 2 rows affected in my `INSERT ... ON DUPLICATE KEY UPDATE`?(为什么在我的`INSERT ... ON DUPLICATE KEY UPDATE`中有 2 行受到影响?)
本文介绍了为什么在我的`INSERT ... ON DUPLICATE KEY UPDATE`中有 2 行受到影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为下表中的 PRIMARY KEY 执行 INSERT ... ON DUPLICATE KEY UPDATE:

I'm doing an INSERT ... ON DUPLICATE KEY UPDATE for a PRIMARY KEY in the following table:

DESCRIBE users_interests;

+------------+---------------------------------+------+-----+---------+-------+
| Field      | Type                            | Null | Key | Default | Extra |
+------------+---------------------------------+------+-----+---------+-------+
| uid        | int(11)                         | NO   | PRI | NULL    |       |
| iid        | int(11)                         | NO   | PRI | NULL    |       |
| preference | enum('like','dislike','ignore') | YES  |     | NULL    |       |
+------------+---------------------------------+------+-----+---------+-------+

但是,即使这些值应该是唯一的,我看到有 2 行受到影响.

However, even though these values should be unique, I'm seeing 2 rows affected.

INSERT INTO users_interests (uid, iid, preference) VALUES (2, 2, 'like')
ON DUPLICATE KEY UPDATE preference='like';

Query OK, 2 rows affected (0.04 sec)

为什么会这样?

编辑

为了比较,请看这个查询:

For comparison, see this query:

UPDATE users_interests SET preference='like' WHERE uid=2 AND iid=2;

Query OK, 1 row affected (0.44 sec)
Rows matched: 1  Changed: 1  Warnings: 0

推荐答案

来自 手册:

使用 ON DUPLICATE KEY UPDATE,如果每行受影响的行值为 1该行作为新行插入,并且 2如果更新了现有行.

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.

这篇关于为什么在我的`INSERT ... ON DUPLICATE KEY UPDATE`中有 2 行受到影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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:按日期将数量值拆分为多行)