从记录中删除小数并使用MySQL查询对其求和

将聚合函数SUM()与REPLACE()一起使用。让我们首先创建一个表-

create table DemoTable857(Price varchar(100));

使用插入命令在表中插入一些记录-

insert into DemoTable857 values('50.60');
insert into DemoTable857 values('40.30.20');
insert into DemoTable857 values('10.20');

使用select语句显示表中的所有记录-

select *from DemoTable857;

这将产生以下输出-

+----------+
| Price    |
+----------+
| 50.60    |
| 40.30.20 |
| 10.20    |
+----------+
3 rows in set (0.00 sec)

以下是删除小数位和总和记录的查询-

select sum(replace(Price,'.','')) AS SumOfALl from DemoTable857;

这将产生以下输出-

+----------+
| SumOfALl |
+----------+
| 409100   |
+----------+
1 row in set (0.03 sec)