MySQL RegExp 用於提取僅包含特定數量單詞的記錄


為此,在 MySQL 中使用正則表示式,語法如下 −

select * from yourTableName where yourColumnName regexp '\land[\land ]+[ ]+[\land ]+$';

當兩個單詞之間用空格分開時,上述查詢將起作用。我們先建立一個表 −

mysql> create table DemoTable1412
   -> (
   -> Name varchar(40)
   -> );
Query OK, 0 rows affected (0.52 sec)

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

mysql> insert into DemoTable1412 values('John Adam Carol');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1412 values('Mike Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1412 values('Chris James Robert');
Query OK, 1 row affected (0.15 sec)

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

mysql> select * from DemoTable1412;

這將產生以下輸出 −

+--------------------+
| Name               |
+--------------------+
| John Adam Carol    |
| Mike Sam           |
| Chris James Robert |
+--------------------+
3 rows in set (0.00 sec)

以下是如何查詢特定行是否包含兩個用空格分隔的單詞 −

mysql> select * from DemoTable1412 where Name regexp '\land[\land ]+[ ]+[\land ]+$';

這將產生以下輸出 −

+----------+
| Name     |
+----------+
| Mike Sam |
+----------+
1 row in set (0.00 sec)

更新於: 2019 年 12 月 16 日

155 次瀏覽

開啟您的職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.