如何在MySQL中用逗号分隔的值之间搜索特定的字符串?
为此,请使用REGEXP。让我们首先创建一个表-
create table DemoTable1902 ( Subjects text );
使用插入命令在表中插入一些记录-
insert into DemoTable1902 values('MongoDB,Java,Python'); insert into DemoTable1902 values('SQL Server,MySQL,PL/SQL'); insert into DemoTable1902 values('Hibernate,Spring,JPA');
使用select语句显示表中的所有记录-
select * from DemoTable1902;
这将产生以下输出-
+-------------------------+ | Subjects | +-------------------------+ | MongoDB,Java,Python | | SQL Server,MySQL,PL/SQL | | Hibernate,Spring,JPA | +-------------------------+ 3 rows in set (0.00 sec)
这是在MySQL中搜索逗号分隔值之间的特定字符串的查询-
select * from DemoTable1902 where Subjects regexp '(^|.*,)MySQL(,.*|$)';
这将产生以下输出-
+-------------------------+ | Subjects | +-------------------------+ | SQL Server,MySQL,PL/SQL | +-------------------------+ 1 row in set (0.00 sec)