MySQL删除小数点(.00)并保留小数点,如果不是.00

时间:2018-04-07 17:30:03

标签: php mysql

如果值为.00,是否有任何方法可以删除小数点,如果不是.00则使用MySQL查询本身保留小数点。

例如: 如果数据是2.56和3.00那么mysql查询应返回2.56& 3

1 个答案:

答案 0 :(得分:2)

修改

我想知道你是否真的在追逐一些不同的东西。所以:

SET @n = 17.00;
SELECT @n, TRIM(TRAILING '.00' FROM CAST(@n AS CHAR)) AS a;
SET @n = 17.30;
SELECT @n, TRIM(TRAILING '.00' FROM CAST(@n AS CHAR)) AS b;
SET @n = 17.32;
SELECT @n, TRIM(TRAILING '.00' FROM CAST(@n AS CHAR)) AS c;

http://sqlfiddle.com/#!9/9eecb/26367

17      17
17.3    17.30
17.32   17.32

这里的区别在于,如果没有小数,则返回整数,其他返回的是(带有尾随零)。

注意,值已经是native floats in MySql are already trimmed,所以我假设这些是字符串。

SELECT
   '17.00' * 1 AS a
 , '17.32' * 1 AS b
 , '17.30' * 1 AS c

http://sqlfiddle.com/#!9/9eecb/26343

产地:

a   b      c
----------------
17  17.32  17.3