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

标签 mysql insert insert-update

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

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 行受到了影响。

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)

为什么会这样?

编辑

为了比较,请参阅此查询:

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

最佳答案

来自 the manual :

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.

关于mysql - 为什么我的 `INSERT ... ON DUPLICATE KEY UPDATE` 中有 2 行受到影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154517/

相关文章:

mysql - 如何通过多对多关系获取表中的所有条目

python - 使用正则表达式将字符串插入文件路径字典中

php - "insert into $schoolname (' cat ',' ...) $_POST "if statement "没有将值传递到 $schoolname 表中

php - 一个查询中的多个 INSERT INTO 但更改了一个字段

php - UpdateAll 在 CakePHP 中不起作用

mysql - zend框架,mysql : How do I Set name utf-8 in application. ini

php - 进行更快的 MySQL PHP 查询

php - 使用 PHP 填充表的最有效方法是什么?

PHP - 列数与第 1 行的值数不匹配

c# - Entity Framework - 使用外键插入