如何搜尋在 MySQL 中逗號分隔值之間的特定字串?


為此,請使用 REGEXP。讓我們首先建立一個表 −

mysql> create table DemoTable1902
   (
   Subjects text
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1902 values('MongoDB,Java,Python');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1902 values('SQL Server,MySQL,PL/SQL');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1902 values('Hibernate,Spring,JPA');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1902;

這將產生以下輸出 −

+-------------------------+
| Subjects                |
+-------------------------+
| MongoDB,Java,Python     |
| SQL Server,MySQL,PL/SQL |
| Hibernate,Spring,JPA    |
+-------------------------+
3 rows in set (0.00 sec)

以下是在 MySQL 中搜索逗號分隔值之間特定字串的查詢 −

mysql> select * from DemoTable1902
   where Subjects regexp '(^|.*,)MySQL(,.*|$)';

這將產生以下輸出 −

+-------------------------+
| Subjects                |
+-------------------------+
| SQL Server,MySQL,PL/SQL |
+-------------------------+
1 row in set (0.00 sec)

更新日期: 2019 年 12 月 27 日

545 次瀏覽

開啟您的事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.