0
我在這裏得到一些非常意外的行爲 - mysql似乎是以我無法解釋的方式做數學錯誤。 TLDR;我在子查詢中選擇了一個double(7, 4)
字段的總和,插入到具有相同數據類型的另一個表的列中,但插入步驟因爲某種原因而使我的數學出錯。這裏是我的:INSERT ... SELECT負面的雙重值損壞
mysql> create temporary table table_1 (amount double(7, 4) not null);
mysql> create temporary table table_2 (amount double(7, 4) not null);
mysql> insert into table_1 (amount) values (-500.00);
mysql> insert into table_1 (amount) values (-500.00);
mysql> insert into table_1 (amount) values (-500.00);
mysql> select sum(amount) from table_1;
+---------------+
| sum(amount) |
+---------------+
| -1500.00|
+---------------+
mysql> insert into table_2 select sum(amount) from table_1;
mysql> select * from table_2;
+-----------------+
| amount |
+-----------------+
| -999.9999 |
+-----------------+
這到底是怎麼回事?我正在使用版本5.5.46-0 + deb8u1。
編輯:改變table_2.amount爲float列修復該問題(我得到-1500最後查詢)
-1500在小數點前有4位數字,但您只用比例/精度(7-4 = 3)指定3。爲什麼不使用更合理的價值? –