您可以使用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)