如何使用 MySQL 从电话号码中提取区号?


假设我们有一个电话号码列表,我们需要从中获取区号。区号一般是电话号码中的前 3 位数字。为此,可以使用 MySQL 的 LEFT() 函数。

首先,让我们创建一个表 -

mysql> create table DemoTable
-> (
-> AreaCodes varchar(100)
-> );
Query OK, 0 rows affected (0.62 sec)

使用 insert 命令在表中插入一些记录。此处,假设我们已经包含电话号码 -

mysql> insert into DemoTable values('90387568976') ;
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('90389097878' ;
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values('56789008799');
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable values('45679008571');
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values('45679008536);
Query OK, 1 row affected (0.12 sec)

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

mysql> select *from DemoTable;

这将产生以下输出 -

+------------+
| AreaCodes  |
+------------+
| 90387568976|
| 90389097878|
| 56789008799|
| 45679008571|
| 45679008536|
+------------+
5 rows in set (0.00 sec)

以下是获取可用区号的查询 -

mysql> select distinct LEFT(AreaCodes,4) from DemoTable

这将产生以下输出 -

+-------------------+
| left(AreaCodes,4) |
+-------------------+
| 9038              |
| 5678              |
| 4567              |
+-------------------+
3 rows in set (0.13 sec)

更新于: 2019 年 7 月 30 日

944 次查看

开启您的事业

通过完成课程获取认证

开始
广告
© . All rights reserved.