mysql - 乘以数字时列的数据被截断

标签 mysql

我正在对以下数据使用以下查询。但是,当我执行此操作时,每列和行都会收到错误“第 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

Database columns Database rows

任何帮助将不胜感激!

最佳答案

strength * .999结果是小数点后 4 位以上的数字。这适用于所有这些计算。

要避免警告,您可以 ROUNDTRUNCATE计算的结果。例如: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/

相关文章:

MySQL SELECT OpenCarts 数据库中的重复行

mysql - 转义拉丁字符以使用 perl 插入和获取数据库

c# - 将 DateTime.Ticks 值转换为 SQL 日期的 MySQL 脚本

Mysql Select 语句不起作用

PHP 在每个页面上包含或复制粘贴 mysql 登录信息?

java - mysql 找不到列循环

MySQL 在选择之前按日期排序

php - 使用PDO的mysql_num_rows的替代方法

带有 IF 语句的 Mysql SUM 函数

MySQL ODBC 5.3 64 位安装