在MySQL中使用正則表示式查詢包含a-z、A-Z和0-9的字串


要查詢包含a-z、A-Z和0-9的字串,請使用BINARY REGEXP以及AND運算子。

讓我們先建立一個表:

mysql> create table DemoTable738 (UserId varchar(100));
Query OK, 0 rows affected (0.81 sec)

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

mysql> insert into DemoTable738 values('John');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable738 values('sAm456');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('98Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('67david');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable738 values('69MIKE');
Query OK, 1 row affected (0.18 sec)

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

mysql> select *from DemoTable738;

這將產生以下輸出:

+---------+
| UserId  |
+---------+
| John    |
| sAm456  |
| 98Carol |
| 67david |
| 69MIKE  |
+---------+
5 rows in set (0.00 sec)

以下是使用REGEXP查詢字串的查詢:

mysql> select *from DemoTable738 where UserId REGEXP BINARY '[A-Z]' 
   and UserId REGEXP BINARY '[a-z]' and UserId REGEXP BINARY '[0-9]';

這將產生以下輸出:

+---------+
| UserId  |
+---------+
| sAm456  |
| 98Carol |
+---------+
2 rows in set (0.03 sec)

更新於:2019年8月22日

439 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.