如何让 Round() 的第二个参数与列一起使用?

How to get second argument of Round() to work with columns?

我有一个带有整数列的简单 table:

# setup table:
create table t(x int); insert t select 1;

查询 select round(1.234, 1) from t returns 1.2 符合预期。

然而,select round(1.234, x) from t returns 1.2000。 (它应该 return 1.2,如 per the docs。)

这是一个错误吗? (在版本 5.5.10latest 5.6.24 上测试。)

或者,是否有任何特殊的技术原因导致不能在round的第二个参数中使用列?

即使第二个参数中使用了列,我们如何让round工作

考虑使用 FORMAT 而不是 ROUND:

mysql> SELECT FORMAT(1.234, x) FROM ( SELECT 1 AS x ) y;
+------------------+
| FORMAT(1.234, x) |
+------------------+
| 1.2              |
+------------------+

如果不满意,file a bugROUND

此错误不仅限于 ROUND(),还发生在 TRUNCATE()

@RickJames 接受的 answer/workaround 并不完全正确,因为 FORMAT() 引入了千位分隔符:FORMAT() documentation

任意值的正确解决方法:

SELECT REPLACE(FORMAT(t.raw_number, t.decimal_places), ',', '')
FROM   my_table t;

(添加我的评论作为单独的答案,因为我没有足够的评论点...)