MySQL中的更新返回"匹配的行数:1已更改:0警告:0"

时间:2015-06-27 07:36:41

标签: mysql

出于某种原因,当我对MySQL中的特定表进行更新时,响应是:

"匹配的行数:1已更改:0警告:0"

我不能为我的生活找出原因。我可以从数据库中进行选择:(我有X'输出敏感数据 - 它只是文本)

mysql> SELECT * FROM outgoings WHERE id=198;
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
| id  | user_id | outgoing_date | form | bsb | account_number | transfer_number | amount    | recipient                          | client_id | project_id | purpose          | created_at          | updated_at          | received_from | cheque_number | cheque_to | project_id_from | cheque_drawer                   | recipient_purpose | transferrer_purpose | client_id_from |
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
| 198 |       2 | 2015-03-11    |    3 |     |                |                 | 407481.25 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |       162 |        503 | XXXXXXXXXXXXXXXX | 2015-03-13 17:51:36 | 2015-03-13 17:51:36 |               | 161           |           |            NULL | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |                   |                     |           NULL |
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
1 row in set (0.00 sec)

如您所见,金额列为407481.25。然后我运行一个简单的更新查询:

mysql> UPDATE cl_time.outgoings SET amount=407481.24 WHERE outgoings.id=198;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

结果显示没有更新,尽管事实上我实际上是想改变一些东西。如果我再次运行select查询,我会看到相同的结果:

mysql> SELECT * FROM outgoings WHERE id=198;
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
| id  | user_id | outgoing_date | form | bsb | account_number | transfer_number | amount    | recipient                          | client_id | project_id | purpose          | created_at          | updated_at          | received_from | cheque_number | cheque_to | project_id_from | cheque_drawer                   | recipient_purpose | transferrer_purpose | client_id_from |
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
| 198 |       2 | 2015-03-11    |    3 |     |                |                 | 407481.25 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |       162 |        503 | XXXXXXXXXXXXXXXX | 2015-03-13 17:51:36 | 2015-03-13 17:51:36 |               | 161           |           |            NULL | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |                   |                     |           NULL |
+-----+---------+---------------+------+-----+----------------+-----------------+-----------+------------------------------------+-----------+------------+------------------+---------------------+---------------------+---------------+---------------+-----------+-----------------+---------------------------------+-------------------+---------------------+----------------+
1 row in set (0.00 sec)

对于更多上下文,这是表结构:

CREATE TABLE `outgoings` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `outgoing_date` date NOT NULL,
  `form` int(11) NOT NULL,
  `bsb` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `account_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `transfer_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `amount` float(8,2) NOT NULL,
  `recipient` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `client_id` int(11) NOT NULL,
  `project_id` int(11) NOT NULL,
  `purpose` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `received_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cheque_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cheque_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `project_id_from` int(11) DEFAULT NULL,
  `cheque_drawer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `recipient_purpose` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `transferrer_purpose` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `client_id_from` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `outgoings_user_id_index` (`user_id`),
  KEY `outgoings_client_id_index` (`client_id`),
  KEY `outgoings_project_id_index` (`project_id`),
  KEY `outgoings_project_id_from_index` (`project_id_from`),
  KEY `outgoings_client_id_from_index` (`client_id_from`)
) ENGINE=MyISAM AUTO_INCREMENT=228 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

最后,这个表的数据是由我在Laravel中编写的PHP应用程序插入并写入的。但是,应用程序永远不会在此表上运行更新查询。所有这一切都工作正常,直到我将应用程序迁移到新服务器,我现在无法运行我的更新。

哦,我的mysql版本是:5.5.41,在Ubuntu上运行。

(这些问题似乎要求大致相同,但是没有一个答案对我有用,所以我在这里询问我的所有数据:Why would rows match but not be changed with a mysql update statement?Update statement in mysql not working, while it has to work

3 个答案:

答案 0 :(得分:3)

更新不会更改行的值的原因是舍入问题。您已将amount定义为float(8,2)。然后,您将值从407481.25更改为407481.24 - 它们之间的差异仅为0.01,但我们知道计算机存在舍入问题。

由于更新没有写入新值,因此可以100%安全地得出结论,由于舍入问题,MySQL将这两个数字视为相同。如果记录没有真正改变,MySQL就不会把它写下来 - 这是一个优化步骤,如果没有必要,MySQL将不会让硬盘进入工作。

现在,这意味着您需要调整amount列类型并将其更改为整数,然后在内部将小数点向左移动两个位置或使用数据类型,例如{{1}具有较大的十进制数字最大数字(例如:decimal)。

答案 1 :(得分:0)

在mysql更新中,当行中的值实际上没有改变时,它不会计入更改的行数(显然)。

"变更"意味着"新值与旧值不同"。

答案 2 :(得分:0)

您可以将数据类型更改为DOUBLE:

alter table outgoings modify amount DOUBLE;