How to search for particular strings between comma separated values in MySQL?

MySQLMySQLi Database

For this, use REGEXP. Let us first create a table −

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

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select * from DemoTable1902;

This will produce the following output −

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

Here is the query to search for particular strings between comma separated values in MySQL −

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

This will produce the following output −

+-------------------------+
| Subjects                |
+-------------------------+
| SQL Server,MySQL,PL/SQL |
+-------------------------+
1 row in set (0.00 sec)
raja
Published on 27-Dec-2019 11:41:07
Advertisements