Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql十进制和tinyint乘法精度_Mysql - Fatal编程技术网

mysql十进制和tinyint乘法精度

mysql十进制和tinyint乘法精度,mysql,Mysql,在mysql 5.1中,我有一个包含两列的表 create table t1 { price decimal(6,2), quantity tinyint(4), ... } 在select查询中,我正在执行 select sum(price * quantity) from t1 group by ... 此计算是否为精确计算(使用定点算法)?或者我需要担心舍入/精度损失等吗?当精度为2的十进制数乘以任何整数(int或tinyint)时,您不必担心会损失任何额外的精度——结果中

在mysql 5.1中,我有一个包含两列的表

create table t1 {
  price decimal(6,2),
  quantity tinyint(4),
  ...
}
在select查询中,我正在执行

select sum(price * quantity)
from t1
group by ...

此计算是否为精确计算(使用定点算法)?或者我需要担心舍入/精度损失等吗?

当精度为2的十进制数乘以任何整数(int或tinyint)时,您不必担心会损失任何额外的精度——结果中的小数位数不会超过2位

我相信小数与整数相乘时没有精确的损失。看见我还做了一个小实验,看看浮点和十进制之间的区别:

SELECT .1E0 * .1E0, 
       .1 * .1, 
       (.1E0 * .1E0) * 10, 
       (.1 * .1) * 10
输出是

0.010000000000000002          << .1E0 * .1E0
0.01                          << .1 * .1
0.10000000000000002           << (.1E0 * .1E0) * 10
0.10                          << (.1 * .1) * 10

0.010000000000000002

例如,请参见此处:

但是你可能会因为你的耳环而惹上麻烦。其范围为[-128127],尽管有(4)。看这里