在 MySQL 中選擇以 5 個數字字元開頭的所有電子郵件地址(正則表示式)


要獲取以 5 個數字字元開頭的電子郵件地址,可選的解決方案是使用 REGEXP −

select *from yourTableName where yourColumnName regexp "^[0-9]{5}";

我們首先建立一個表 −

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

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

mysql> insert into DemoTable values('6574John@gmail.com');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Carol23456@gmail.com');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('98989Chris_45678@gmail.com');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Mike12@gmail.com');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values('56453Adam@gmail.com');
Query OK, 1 row affected (0.20 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 6574John@gmail.com         |
| Carol23456@gmail.com       |
| 98989Chris_45678@gmail.com |
| Mike12@gmail.com           |
| 56453Adam@gmail.com        |
+----------------------------+
5 rows in set (0.00 sec)

以下是選擇以 5 個數字字元開頭的所有電子郵件地址的查詢 −

mysql> select *from DemoTable where UserEmailAddress regexp "^[0-9]{5}";

這將產生以下輸出 −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 98989Chris_45678@gmail.com |
| 56453Adam@gmail.com        |
+----------------------------+
2 rows in set (0.00 sec)

更新時間: 2019 年 10 月 7 日

178 次瀏覽

開啟你的 職業生涯

完成課程取得認證

開始
廣告
© . All rights reserved.