【发布时间】:2014-04-20 08:24:09
【问题描述】:
我有下表结构和数据:
create table sample
(
id INT(10)
);
INSERT INTO sample
values
(23398),
(98743),
(54734);
现在我想了解mysql中的CAST函数。考虑以下查询:
select
cast((id/3) as decimal(2,2)) as cast1,
cast((id/3) as decimal(3,2)) as cast2,
cast((id/3) as decimal(4,2)) as cast3,
cast((id/3) as decimal(5,2)) as cast4,
cast((id/3) as decimal(6,2)) as cast5,
cast((id/3) as decimal(7,2)) as cast6,
id/3 as actualId
from sample;
请在SQL Fiddle查看此查询的输出。
我想知道为什么这个查询会给出0.99、9.99,反之亦然。
谁能解释一下?
提前致谢。
【问题讨论】:
-
它只是在给定的空间内给你最接近的数字
-
对于
cast((id/3) as decimal(2,2)) as cast1,转换,0.99 如何成为最接近 23398 的数字? -
你期望输出什么?
-
我只是想了解为什么这种类型转换会给我像
0.99、9.99这样的结果,反之亦然。 -
你知道0.99是decimal(2,2)能有的最大数吧?
标签: mysql sql int type-conversion decimal