MySQL INSERT 显示的受影响行数多于受影响行数

标签 mysql

考虑以下三个查询。

第一个只能返回一行,因为 bids_buy.id 是主键。

第二个显示了 entities_has_documents 中主键 3099541982-2536988132 的现有记录,而第三个则由于该现有记录而不会执行。

第四个确实按预期执行,但显示了两行受影响的行。

为什么它显示两行受影响的行,而不仅仅是与主键 3099541982-2536988132 关联的行?

mysql> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798;
+--------------+------------+----------+---------------------+-----------+
| bids_sell_id | 2536988132 | pub_bids | NOW()               | 506836355 |
+--------------+------------+----------+---------------------+-----------+
|   3099541982 | 2536988132 | pub_bids | 2016-04-16 08:19:13 | 506836355 |
+--------------+------------+----------+---------------------+-----------+
1 row in set (0.00 sec)

mysql> SELECT * FROM entities_has_documents;
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
| entities_id | documents_id | type     | date_added          | date_removed | added_by  | removed_by | purged_by | date_purged |
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
|  2453409798 |   2536988132 | pub_bids | 2016-04-16 08:07:13 | NULL         | 506836355 |       NULL |      NULL | NULL        |
|  3099541982 |   2536988132 | pub_bids | 2016-04-16 08:18:53 | NULL         | 506836355 |       NULL |      NULL | NULL        |
+-------------+--------------+----------+---------------------+--------------+-----------+------------+-----------+-------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO entities_has_documents(entities_id,documents_id,type,date_added,added_by)
    -> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798;
ERROR 1062 (23000): Duplicate entry '3099541982-2536988132' for key 'PRIMARY'

mysql> INSERT INTO entities_has_documents(entities_id,documents_id,type,date_added,added_by)
    -> SELECT bb.bids_sell_id, 2536988132,"pub_bids", NOW(),506836355 FROM bids_buy bb WHERE bb.id=2453409798
    -> ON DUPLICATE KEY UPDATE type="pub_bids", added_by=506836355, date_added=NOW(), removed_by=NULL, date_removed=NULL;
Query OK, 2 rows affected (0.00 sec)
Records: 1  Duplicates: 1  Warnings: 0

mysql> EXPLAIN bids_buy;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| id           | int(10) unsigned | NO   | PRI | NULL    |       |
| bids_sell_id | int(10) unsigned | NO   | MUL | NULL    |       |
| stage_buy_id | int(10) unsigned | NO   | MUL | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> EXPLAIN entities_has_documents;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| entities_id  | int(10) unsigned | NO   | PRI | NULL    |       |
| documents_id | int(10) unsigned | NO   | PRI | NULL    |       |
| type         | varchar(16)      | NO   | MUL | NULL    |       |
| date_added   | datetime         | NO   |     | NULL    |       |
| date_removed | datetime         | YES  |     | NULL    |       |
| added_by     | int(10) unsigned | NO   | MUL | NULL    |       |
| removed_by   | int(10) unsigned | YES  | MUL | NULL    |       |
| purged_by    | int(10) unsigned | YES  | MUL | NULL    |       |
| date_purged  | datetime         | YES  |     | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
9 rows in set (0.01 sec)

mysql>

编辑

http://php.net/manual/en/pdostatement.rowcount.php

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

那么,我是否只看到从 SELECT 语句返回的数字或行,而不是受 INSERT 影响的数字或行? MySQL 为什么要这么做?

编辑完成

最佳答案

我认为这是由于 ON DUPLICATE KEY UPDATE 修饰符作为 MYSQL Reference Manual 5.5以及MySQL Reference Manual 5.7

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. The affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is 1 (not 0) if an existing row is set to its current values.”.

在您的情况下,您已经有一行具有主键值3099541982-2536988132。因此,MySQL 通过指示受影响的 2 行来让您知道您正在尝试插入具有重复主键或唯一键的行。正如手册中还指出的那样,在出现重复 key 的情况下,ON DUPLICATE KEY UPDATE 会导致 INSERT 序列,而不是 UPDATE 更新命令,而它只执行如果 key 不存在,则使用 INSERT 命令。

我希望这会有所帮助。

更新

另请参阅此 link .

关于MySQL INSERT 显示的受影响行数多于受影响行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36666044/

相关文章:

mysql - 我如何在 SQL 中使用 LIKE 函数而不是一个确切的词?

mysql:使用一个字段包含多个 "fields"来保存字段

mysql - C中一个sql语句中的多个插入

javascript - 获取下拉菜单特定的选定值并显示确认框

php - 显示第一个选中的行 Mysql

mysql - XAMPP MySQL 数据库无法运行

mysql - 有限获取所需文件

mysql - 用于插入在 1 中大于最大值的值的 SQL 命令

php - 如何在 Laravel Eloquent 中将表连接到表中

php - 将一列指向另一个表中的列