0

我有这个 mysql 列:

points_on | points_off
    0          1.36

而这个 SQL 命令:

UPDATE table SET points_off = points_off-{$cash}, points_on = points_on+{$cash} WHERE ...

$现金=“1.36”

如果我运行它,表更新错误如下:

points_on | points_off
   1.36      0.0000000143051 

如何正确更新?

我需要将$cash号码从 points_off 移动到 points_on。

4

3 回答 3

3

是 MySQL 字段类型float还是double

根据http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html,问题是float存储的值是近似的。

您应该使用decimal, 定义为decimal(M,D), 其中M是数字中的最大位数(例如,对于 0.00 到 9.99,M = 3),并且D是点之后的位数(例如,对于 0.00 到 9.99,D = 2)。

我执行了您尝试过的相同操作,遇到了相同的问题,并使用decimal.

请注意,尽管decimal字段需要的存储空间比float.

于 2013-11-06T10:03:52.270 回答
0

我相信这源于 PHP 在内部如何表示数字。据我所知,PHP 将数字存储为浮点小数,这可能会生成奇怪的输出,例如您的输出。

我建议阅读以下 StackOverflow 文章:

不确定它是否能 100% 工作,但您可能想尝试实现一些代码以确保您的数学运算精确到小数点后两位,而不是让 PHP 决定。参考的文章可能会有所帮助。

于 2013-11-06T10:01:27.863 回答
0
declare @cash float

set @cash=1.36
update  [dbo].[stkkk] set [pts_on]=[pts_on]+@cash

update  [dbo].[stkkk] set [pts_off]=[pts_off]-@cash
于 2013-11-06T09:49:50.597 回答