MySQL 如何从字符串中提取数字部分
首先让我们创建一个表 –
mysql> create table DemoTable
-> (
-> StudentId varchar(100)
-> );
查询OK,受影响的行数为0(0.71秒)
使用插入命令在表中插入一些记录 –
mysql> insert into DemoTable values('John19383');
查询OK,受影响的行数为1(0.18秒)
mysql> insert into DemoTable values('Carol9999');
查询OK,受影响的行数为1(0.16秒)
mysql> insert into DemoTable values('David123456');
查询OK,受影响的行数为1(0.15秒)
使用select语句从表中显示所有记录 –
mysql> select *from DemoTable;
阅读更多:MySQL 教程
输出
这将产生以下输出 –
+-------------+
| StudentId |
+-------------+
| John19383 |
| Carol9999 |
| David123456 |
+-------------+
共3行(0.00秒)
以下是从字符串中提取数字部分的查询 –
mysql> SELECT replace(reverse(FORMAT(reverse(StudentId),0)), ',', '') as OnlyDigit from DemoTable;
输出
这将产生以下输出 –
+-----------+
| OnlyDigit |
+-----------+
| 19383 |
| 9999 |
| 123456 |
+-----------+
共3行(0.05秒)
极客教程