mysql - MySQL 5.7 中截断了不正确的 DECIMAL 值

标签 mysql decimal mysql-error-1292

在 SQL 模式“STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”的 MySQL 5.6.13 中,以下查询工作正常,但有警告:

UPDATE Company SET company_CurrentYearValueGBP = COALESCE((
SELECT SUM(
(COALESCE(salesdata_Month01Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month02Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month03Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month04Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month05Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month06Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month07Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month08Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month09Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month10Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month11Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
(COALESCE(salesdata_Month12Amount,0) / IF(salesdata_ExchangeRate<>'',salesdata_ExchangeRate,1) ) + 
0) FROM SalesData 
WHERE salesdata_SalesDataTypeID = 3 
AND salesdata_CompanyID = company_ID 
AND salesdata_SalesYearID = 6 
AND NOT salesdata_IsBeingProcessed 
),0) 

输出:

0 row(s) affected, 5 warning(s):
 1265 Data truncated for column 'company_CurrentYearValueGBP' at row 127
 1265 Data truncated for column 'company_CurrentYearValueGBP' at row 127
 1265 Data truncated for column 'company_CurrentYearValueGBP' at row 127
 1265 Data truncated for column 'company_CurrentYearValueGBP' at row 127
 1265 Data truncated for column 'company_CurrentYearValueGBP' at row 127
 Rows matched: 1470  Changed: 0  Warnings: 5

在具有 SQL 模式“STRICT_TRANS_TABLES”的 MySQL 5.7.14 中,相同的查询会产生以下错误:

Error Code: 1292. Truncated incorrect DECIMAL value: ''

我明白为什么会发生错误(因为比较 salesdata_ExchangeRate<>''),但是任何人都可以解释为什么在 MySQL 5.6 中即使启用了严格模式,查询也只产生警告而不是因错误而中止?两个版本之间的行为似乎有所不同。

更新 1 在 MySQL 5.6.13 和 MySQL 5.7.14 上创建了一个具有以下结构的简单表:

Structure

添加了以下记录:

Records

运行以下查询会在 MySQL 5.7.14 中重现相同的错误:

UPDATE testtable 
SET testtable_CalculatedValue = IF(testtable_DecimalValue<>'',testtable_DecimalValue,0) 
WHERE testtable_ID > 0

Error Code: 1292. Truncated incorrect DECIMAL value: ''

在 MySQL 5.6.13 中,testtable_CalculatedValue 按预期设置为 1.35。

最佳答案

设置:

mysql> CREATE TABLE so53868586 (
         id INT AUTO_INCREMENT,
         deci  DECIMAL(12,2) DEFAULT NULL,
         calc DECIMAL(12,2) DEFAULT NULL,
         PRIMARY KEY(id)
     ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)

mysql> SET @@sql_mode = "STRICT_TRANS_TABLES";
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO so53868586 (deci)
         VALUES (1.35), (NULL), (0), (12345);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

测试

mysql> SELECT * FROM so53868586;
+----+----------+------+
| id | deci     | calc |
+----+----------+------+
|  1 |     1.35 | NULL |
|  2 |     NULL | NULL |
|  3 |     0.00 | NULL |
|  4 | 12345.00 | NULL |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> 
mysql> UPDATE so53868586 SET calc = IF(deci <> 0, deci, 0)
         WHERE id > 0;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> 
mysql> SELECT * FROM so53868586;
+----+----------+----------+
| id | deci     | calc     |
+----+----------+----------+
|  1 |     1.35 |     1.35 |
|  2 |     NULL |     0.00 |
|  3 |     0.00 |     0.00 |
|  4 | 12345.00 | 12345.00 |
+----+----------+----------+
4 rows in set (0.01 sec)

(使用的是 5.6.22。)

5.7.15 得到了相同的结果,但提示 SET @sql_mode with

mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                 |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 3135 | 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. |
| Warning | 3090 | Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.                                                                          |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

我是否未能重现您的测试用例?

我在 5.7.15(或附近的任何地方)的变更日志中没有看到任何相关内容,但您可以考虑更新。 (5.7.14 已于 2 年前发布。)

关于mysql - MySQL 5.7 中截断了不正确的 DECIMAL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53868586/

相关文章:

php - 使用具有多个类的静态 transactionCounter 管理事务是正确的方法吗?

javascript - JS 智能舍入小数

javascript - 将小数转换为分数时出现意外结果

mysql - 日期时间不正确。我做错了什么?

MySQL 1292 关于 varchar 日期值到数字转换的警告

c# - Code First with MySQL 不会创建表

MySQL 通过乘以其他行中的值返回 1 或 0

mysql - 在 MariaDB 中监视具有许多更新的 SQL 脚本的进度

c# - 将 double 转换为十进制

mysql - 错误代码 1292 日期不正确 |时间