我有一个带小数(16,2)字段的mysql表.看起来像使用另一个十进制(16,2)字段字符串的加法操作可能会导致第1行问题的列x截断数据,这会在我的django项目中引发异常.
我知道该字段的乘法或除法运算会导致此问题,因为结果可能不适合十进制(16,2)定义,但加法和减法操作是否相同?
我的MySQL服务器版本是5.5.37-0ubuntu0.14.04.1.您可以从下面重现此问题:
MysqL> drop database test;
Query OK,1 row affected (0.10 sec)
MysqL> create database test;
Query OK,1 row affected (0.00 sec)
MysqL> use test;
Database changed
MysqL> create table t(price decimal(16,2));
Query OK,0 rows affected (0.16 sec)
MysqL> insert into t values('2004.74');
Query OK,1 row affected (0.03 sec)
MysqL> select * from t;
+---------+
| price |
+---------+
| 2004.74 |
+---------+
1 row in set (0.00 sec)
MysqL> update t set price = price + '0.09';
Query OK,1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MysqL> update t set price = price + '0.09';
Query OK,1 row affected,1 warning (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 1
MysqL> show warnings;
+-------+------+--------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------+
| Note | 1265 | Data truncated for column 'price' at row 1 |
+-------+------+--------------------------------------------+
1 row in set (0.00 sec)
MysqL> select * from t;
+---------+
| price |
+---------+
| 2004.92 |
+---------+
1 row in set (0.00 sec)
最佳答案
原文链接:https://www.f2er.com/mysql/433480.html