当我把xx添加到mysql float列时,结果是错误的,这是一个bug吗?
我的mysql: 5.6.16 我的表格信息:当我把xx添加到mysql float列时,结果是错误的,这是一个bug吗?,mysql,sql,sql-update,Mysql,Sql,Sql Update,我的mysql: 5.6.16 我的表格信息: CREATE TABLE `xxx` ( `uid` int(11) NOT NULL, `money` float(10,2) NOT NULL DEFAULT '0.00' , `real_money` float(10,2) NOT NULL , `available_invoice` float(10,2) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB DEFAULT
CREATE TABLE `xxx` (
`uid` int(11) NOT NULL,
`money` float(10,2) NOT NULL DEFAULT '0.00' ,
`real_money` float(10,2) NOT NULL ,
`available_invoice` float(10,2) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
我的表格内容是:
1 100000.00 0.00 0.01
30 99992560.00 0.03 0.00
61 65216.00 0.03 0.00
79 10.00 0.00 0.00
80 10.00 0.00 0.00
81 -70.00 0.00 0.00
83 60.00 0.00 0.00
100 100.00 50.00 50.00
889 980.00 0.00 0.00
1234 99959040.00 0.00 99999.00
1239 40.00 0.00 0.00
当我执行sql时:
update `xxx` set `money`=`money`-20 where uid = 1234
结果是:
uid money real_money available_invoice
1 100000.00 0.00 0.01
30 99992560.00 0.03 0.00
61 65216.00 0.03 0.00
79 10.00 0.00 0.00
80 10.00 0.00 0.00
81 -70.00 0.00 0.00
83 60.00 0.00 0.00
100 100.00 50.00 50.00
889 980.00 0.00 0.00
1234 99959024.00 0.00 99999.00
1239 40.00 0.00 0.00
无论我对接近100000000的浮点数加上或减去什么,总是缺少4或2,这是为什么?您的问题与您的浮点数有多大有关,但我不能100%确定原因。我测试了用较小的数字更新类似的行,效果很好。您还应该尽可能避免使用引号,因为它需要额外的处理。见本问题: 这是我的简单工作,就像Tim Biegeleisen建议的那样:
CREATE TABLE `xxx` (
`uid` int(11) NOT NULL,
`money` decimal(10,2) NOT NULL DEFAULT '0.00' ,
`real_money` float(10,2) NOT NULL ,
`available_invoice` float(10,2) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
插入:
USE dbname;
insert into xxx Values(1234, 99959040.00, 0.00, 99999.00);
不带字符串的更新:
update xxx set money = money - 20 where uid = 1234;
使用大浮点值进行计算时,会出现一些奇怪的情况。其中一些问题与机器有关,并且与您拥有的处理器类型有关。请在此处阅读更多信息:
另外,根据另一个SO问题,浮点值不是存储货币值的好方法。(见第二个答案)
似乎在mysql中使用
decimal
和numeric
数据类型最适合于money列。我没有确切的答案,但是使用decimal(10,2)
可能会解决这个问题。有关详细信息,请参阅。是否有插入脚本?所以我可以很容易地重现你的问题?@gordlonious插入xxx
(uid
,money
,real\u money
,available\u invoice
)值(1,100000.00,0.00,0.01),(30,312.00,0.03,0.00),(61,65216.00,0.03,0.00),(79,10.00,0.00,0.00),(81,-70.00,0.00,0.00),(83,60.00,0.00,0.00),(100,100.00,50.00,50.00),(889,980.00,0.00,0.00),(1234,100000.00,0.00,99999.00),(1239,40.00,0.00,0.00);货币几乎总是十进制的——它运行良好!!