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

时间:2016-04-16 15:27:45

标签: 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

  

如果关联的PDOStatement执行的最后一条SQL语句是   一个SELECT语句,一些数据库可能会返回行数   由该声明返回。但是,不保证这种行为   适用于所有数据库,不应依赖于便携式数据库   应用

那么,我只看到SELECT语句返回的数字或行,而不是我的INSERT影响的数量或行数?为什么MySQL会做这样的事情?

编辑完成

1 个答案:

答案 0 :(得分:1)

我认为这是由于.hovered { cursor: pointer; }修饰符与MYSQL Reference Manual 5.5以及MySQL Reference Manual 5.7所说的

  

如果指定ON DUPLICATE KEY UPDATE,并且插入的行将导致UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行的UPDATE。如果将行作为新行插入,则每行的受影响行值为1;如果更新现有行,则为2,如果现有行设置为其当前值,则为0。如果在连接到mysqld时将CLIENT_FOUND_ROWS标志指定给mysql_real_connect(),则如果将现有行设置为其当前值,则affected-rows值为1(不为0)。“

在您的情况下,您已经有一行主键值<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <div class="graphic-content"></div>。因此,MySQL通过指示受影响的2行,让您知道您正在尝试插入具有重复主键或唯一键的行。正如手册中还指出ON DUPLICATE KEY UPDATE在重复键的情况下导致3099541982-2536988132而不是ON DUPLICATE KEY UPDATE更新命令的序列,而在密钥是的情况下它只执行INSERT命令不存在。

我希望这会有所帮助。

<强>更新

另见link