我正在对以下数据使用以下查询。但是,当我执行此操作时,每列和行都会收到错误“第 1 行的列“强度”的数据被截断”。我对此进行了一些研究,据我所知,大多数人都会收到此错误,因为他们尝试使用文本或字符。我以前从未见过此警告,但我得到了预期的结果,但有 4,700 个警告。
UPDATE userstats
SET strength = (strength * .999),
agility = (agility * .999),
guard = (guard * .999),
labour = (labour * .999),
IQ = (IQ * .999)
WHERE gym_train_since_cron = 0
任何帮助将不胜感激!
最佳答案
strength * .999
结果是小数点后 4 位以上的数字。这适用于所有这些计算。
要避免警告,您可以 ROUND或TRUNCATE计算的结果。例如:SET strength = ROUND(strength * .999, 4)
或SET strength = TRUNCATE(strength * .999, 4)
.
您可能想知道这两个函数有什么区别;这是舍入行为。对于 TRUNCATE
它将数字向 0 舍入,而 ROUND
,根据您的情况为十进制(精确类型)的数字数据类型(精确或近似),会发生以下情况(取自 Rounding Behavior ):
For exact-value numbers, ROUND() uses the “round half up” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer if negative.
为了进行演示,下面是一个使用示例数据中用户 4898 的强度值的示例:
strength * .999 = 16331143.7521566 -- Over 4 decimal places, hence the warning
ROUND(strength * .999, 4) = 16331143.7522 -- Rounds up
TRUNCATE(strength * .999, 4) = 16331143.7521 -- Rounds down
关于mysql - 乘以数字时列的数据被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019799/