以下减法查询在小数点后返回太多数字。
mysql> select AA, BB, AA - BB, AA - 23.057, 22.659 - 23.057 from table1 where TID = '1';
+----------+----------+---------------------+---------------------+-----------------+
| AA | BB | AA - BB | AA - 23.057 | 22.659 - 23.057 |
+----------+----------+---------------------+---------------------+-----------------+
| 22.659 | 23.057 | -0.3979999999999997 | -0.3979999999999997 | -0.398 |
+----------+----------+---------------------+---------------------+-----------------+
1 row in set (0.00 sec)
mysql>
我希望我可以使用一个十进制字段,但是用户希望保持输入时在AA和BB列中插入的精度,这就是为什么将VARCHAR用于这些列的原因。
e.g.: 11.10 - 11.22 = - 0.02
11.20 - 11.10 = + 0.10
22.659 - 23.057 = -0.398
23.659 - 22.559 = 0.100
while 23.659 - 22.559 = 0.1 is wrong
任何帮助将不胜感激。谢谢
您可以使用以下format()
功能:
select format(col1 - col2, length(substring_index(col1, '.', -1)))
如果您不喜欢大数字的逗号:
select replace(format(col1 - col2, length(substring_index(col1, '.', -1))), ',', '')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句