如何通过后缀货币符号添加 MySQL 中的列值


为此,您可以使用聚合函数 SUM()。我们首先创建一个表

mysql> create table DemoTable616 (Price varchar(100));
Query OK, 0 rows affected (0.59 sec)

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

mysql> insert into DemoTable616 values('€200.00');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable616 values('€300.00');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable616 values('€500.00');
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from DemoTable616;

这会产生以下输出 -

+-----------+
| Price     |
+-----------+
| €200.00   |
| €300.00   |
| €500.00   |
+-----------+
3 rows in set (0.00 sec)

以下是添加列值查询。在 sum() 方法中,我们替换了货币符号,以便我们可以轻松添加列值 -

.mysql> select sum(replace(Price, '€', '')) as Total_Price from DemoTable616;

这会产生以下输出 -

+-------------+
| Total_Price |
+-------------+
| 1000        |
+-------------+
1 row in set (0.00 sec)

更新于: 2019 年 8 月 23 日

177 浏览

开启您的职业生涯 之旅

完成课程以获得认证

开始
广告
© . All rights reserved.