如何在 MySQL 中从字符串中获取最后 12 位数字?


可以使用 MySQL 中的 RIGHT() 函数从字符串中获取最后 12 位数字。我们首先创建一个表 -

mysql> create table DemoTable
   (
   Number varchar(200)
   );
Query OK, 0 rows affected (0.59 sec)

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

mysql> insert into DemoTable values('7437647847847474374747464647484949959958484');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('9990000399494959697080800007007070808080808');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('1211111212344554444443333345555554433333333333333');
Query OK, 1 row affected (0.14 sec)

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

mysql> select * from DemoTable;

这将产生以下输出 -

+---------------------------------------------------+
| Number                                            |
+---------------------------------------------------+
| 7437647847847474374747464647484949959958484       |
| 9990000399494959697080800007007070808080808       |
| 1211111212344554444443333345555554433333333333333 |
+---------------------------------------------------+
3 rows in set (0.00 sec)

以下是 MySQL 中从字符串中获取最后 12 位数字的查询 -

mysql> select right(Number,12) as `Last 12 Digit From String` from DemoTable;

这将产生以下输出 -

+---------------------------+
| Last 12 Digit From String |
+---------------------------+
| 949959958484              |
| 070808080808              |
| 333333333333              |
+---------------------------+
3 rows in set (0.00 sec)

更新于: 2019-07-30

754 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.