在MySQL中随着长度的变化删除小数点后的零?
您可以使用TRIM()函数删除尾随零。语法如下。
SELECT TRIM(yourColumnName)+0 FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table removeTrailingZeroInDecimal -> ( -> Id int not null auto_increment, -> Amount decimal(5,2), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into removeTrailingZeroInDecimal(Amount) values(405.50); mysql> insert into removeTrailingZeroInDecimal(Amount) values(23.05); mysql> insert into removeTrailingZeroInDecimal(Amount) values(12.050); mysql> insert into removeTrailingZeroInDecimal(Amount) values(125.23); mysql> insert into removeTrailingZeroInDecimal(Amount) values(125.00); mysql> insert into removeTrailingZeroInDecimal(Amount) values(126);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from removeTrailingZeroInDecimal;
以下是输出。
+----+--------+ | Id | Amount | +----+--------+ | 1 | 405.50 | | 2 | 23.05 | | 3 | 12.05 | | 4 | 125.23 | | 5 | 125.00 | | 6 | 126.00 | +----+--------+ 6 rows in set (0.00 sec)
这是删除十进制尾随零的查询。查询如下-
mysql> SELECT TRIM(Amount)+0 FROM removeTrailingZeroInDecimal;
输出显示没有尾随零的记录。
+----------------+ | TRIM(Amount)+0 | +----------------+ | 405.5 | | 23.05 | | 12.05 | | 125.23 | | 125 | | 126 | +----------------+ 6 rows in set (0.00 sec)
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便