用於匹配一個在搜尋中忽略字元的模式的 MySQL Regex,比如 Chris.Brown?


讓我們先建立一個表 -

mysql> create table DemoTable
(
   Name varchar(40)
)
;
Query OK, 0 rows affected (0.63 sec)

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

mysql> insert into DemoTable values('John.Smith');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Chris.Brown');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values('Chris.Taylor');
Query OK, 1 row affected (0.18 sec)

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

mysql> select *from DemoTable;

這將生成以下輸出 -

+--------------+
| Name         |
+--------------+
| John.Smith   |
| Chris Brown  |
| Chris.Brown  |
| David Miller |
| Chris.Taylor |
+--------------+
5 rows in set (0.00 sec)

以下是用於忽略搜尋中的字元的查詢 -

mysql> select *from DemoTable where replace(Name,'.','')='Chris Brown';

這將生成以下輸出 -

+-------------+
| Name        |
+-------------+
| Chris Brown |
+-------------+
1 row in set (0.00 sec)

更新於: 09-10-2019

118 次瀏覽

開啟您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.