MySQL 中使用 REGEX 僅顯示用連字元分隔的數字。


我們先建立一個表 −

mysql> create table DemoTable
   (
      Code varchar(100)
   );
Query OK, 0 rows affected (1.16 sec)

使用 insert 命令在表中插入一些記錄-

mysql> insert into DemoTable values('100-677-9876');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('100-677-9876-John');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('David-100-677-9876');
Query OK, 1 row affected (0.16 sec)

使用 select 語句從表中顯示所有記錄 −

mysql> select *from DemoTable;

這會生成以下輸出 −

+--------------------+
| Code               |
+--------------------+
| 100-677-9876       |
| 100-677-9876-John  |
| David-100-677-9876 |
+--------------------+
3 rows in set (0.00 sec)

以下正則表示式僅顯示用連字元分隔的數字 −

mysql> select *from DemoTable where Code REGEXP '^\(?[0-9]{3}\)?[\s-]?[0-9]{3}[\s-]?[0-9]{4}$';

這會生成以下輸出 −

+--------------+
| Code         |
+--------------+
| 100-677-9876 |
+--------------+
1 row in set (0.00 sec)

更新日期:2019 年 8 月 22 日

358 次瀏覽

啟動您的職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.