如何使用 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.