通过从 MySQL 中删除字符串中的第一个字符后跟数字来求和?
mysqlmysqli database更新于 2025/10/30 13:00:17
字符串(列值)以字符开头,字符串的其余部分包含数字。我们想要这些数字的总和 −
J230 A130s C13
为此,请使用 SUBSTRING() 函数和 SUM()。
首先我们创建一个表 −
mysql> create table DemoTable761 (Price varchar(100)); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable761 values('J230');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable761 values('A130');
Query OK, 1 row affected (0.70 sec)
mysql> insert into DemoTable761 values('C13');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable761 values('D456');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable761 values('B6');
Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable761;
这将产生以下输出 -
+-------+ | Price | +-------+ | J230 | | A130 | | C13 | | D456 | | B6 | +-------+ 5 rows in set (0.00 sec)
下面是通过从 MySQL 中删除字符串中的第一个字符后跟数字来查找总和的查询 −
mysql> select sum(substring(Price,2)) from DemoTable761;
这将产生以下输出 -
+-------------------------+ | sum(substring(Price,2)) | +-------------------------+ | 835 | +-------------------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

