当我查询SELECT 1*7.6
时,它会让我感到7.6
但当我进行列乘法运算时(products.commission*salled.amount):
SELECT *,
(products.commision*sold.amount) as fee
FROM sold
RIGHT JOIN products
ON sold.idprod=products.ID
AND DATE(sold.date) BETWEEN DATE('2015-10-01') AND DATE('2015-10-31')
WHERE sold.userid="1"
ORDER BY sold.userid
我很高兴
ID | userid | date | idprod | amount | Category | Name | commision | fee
60 | 1 | 15-10-01 | 21 | 1 | Volish | Black | 7.6(float) | 7.599999904632568
为什么?
最佳答案
在进行比较时,必须将值类型转换为整数/小数,而不是浮点
(CAST(products.commision AS Decimal(7,2)) * CAST(sold.amount AS Decimal(7,2))) as fee
从https://dev.mysql.com/doc/refman/5.0/en/cast-functions.html和casting a floating to decimal in mysql
十进制将值设为十进制,用7位表示最大大小和2位数的最大精度(12345.67)。
编辑:我不确定将CAST添加到原始值还是简单地将答案转换为
(products.commision * sold.amount ) as CAST(fee AS Decimal(7,2))