用於查詢包含 N 個分號的行


我們首先建立一個表 -

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ,
   Title text
);
Query OK, 0 rows affected (0.88 sec)

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

mysql> insert into DemoTable(Title) values('This is; a; MySQL;Tutorial');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Title) values('Java is; an;Object Oriented');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(Title) values('MongoDB ; is;a; database');
Query OK, 1 row affected (0.12 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+----+-----------------------------+
| Id | Title                       |
+----+-----------------------------+
|  1 | This is; a; MySQL;Tutorial  |
|  2 | Java is; an;Object Oriented |
|  3 | MongoDB ; is;a; database    |
+----+-----------------------------+
3 rows in set (0.00 sec)

以下正則表示式可用於查詢包含 3 個分號的行 -

mysql> select *from DemoTable where Title REGEXP '(.*;){3}';

這將產生以下輸出 -

+----+----------------------------+
| Id | Title                      |
+----+----------------------------+
|  1 | This is; a; MySQL;Tutorial |
|  3 | MongoDB ; is;a; database   |
+----+----------------------------+
2 rows in set (0.00 sec)

更新日期: 09-Oct-2019

186 次瀏覽

開啟您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.