我不太明白cast
功能和decimal
这里。什么是decimal(7,2)
实际上是什么意思?The first part and the second part arg of the function.
为什么我需要cast/convert
我的浮点到小数在这里?
mysql> SELECT productCode, MAX(price), MIN(price),
CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`,
CAST(STD(price) AS DECIMAL(7,2)) AS `Std Dev`,
SUM(quantity)
FROM products
GROUP BY productCode;
+-------------+------------+------------+---------+---------+---------------+
| productCode | MAX(price) | MIN(price) | Average | Std Dev | SUM(quantity) |
+-------------+------------+------------+---------+---------+---------------+
| PEC | 0.49 | 0.48 | 0.49 | 0.01 | 18000 |
| PEN | 1.25 | 1.23 | 1.24 | 0.01 | 15000 |
+-------------+------------+------------+---------+---------+---------------+
下面是一个 sql 小提琴吗?
http://sqlfiddle.com/#!2/1ed51b/1/0 http://sqlfiddle.com/#!2/1ed51b/1/0
我的问题再次重复:
- 什么是
decimal(7,2)
实际上是什么意思?The first part and the
second part arg of the function.
- 为什么我需要
cast/convert
我的浮点到小数在这里?
DECIMAL(7,2)
表示具有固定小数点的数字,共有 7 位数字,其中 2 位位于小数点右侧。 (所以,左 5 个,右 2 个。)
你不need除非您想要 DECIMAL 类型的行为(通常是定点)而不是 FLOAT 类型的行为(小数点右侧位数相对不可预测的有用近似值)。例如,产品代码“PEC”的平均值with演员阵容为33333.65;without演员阵容是 33333.653333。
如果您经常使用all数字,您可能应该增加演员阵容中的数字总数。说,像这样的事情DECIMAL (14,2)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)