是否可以在 MySQL Select 語句中允許正則匹配?


是的,我們可以在 select 語句中進行正則匹配 −

select yourColumnName from yourTableName where yourColumnName regexp '^yourValue';

我們首先建立一個表 −

mysql> create table DemoTable1892
   (
   FirstName varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1892 values('John');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1892 values('Adam');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1892 values('Jace');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1892 values('Johny');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1892 values('Johnson');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1892 values('Jack');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1892;

這將生成以下輸出 −

+-----------+
| FirstName |
+-----------+
| John      |
| Adam      |
| Jace      |
| Johny     |
| Johnson   |
| Jack      |
+-----------+
6 rows in set (0.00 sec)

以下是 MySQL select 語句中正則匹配的查詢 −

mysql> select FirstName from DemoTable1892 where FirstName regexp '^Jo';

這將生成以下輸出 −

+-----------+
| FirstName |
+-----------+
| John      |
| Johny     |
| Johnson   |
+-----------+
3 rows in set (0.00 sec)

更新於: 2019 年 12 月 27 日

117 次檢視

開啟您的 職業

完成課程獲取認證

開始
廣告
© . All rights reserved.