How can I search within a table of comma-separated values in MySQL?

MySQLMySQLi Database

To search within a table of comma-separated values, use LIKE operator. Let us first create a table −

mysql> create table DemoTable675(Value text);
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable675 values('10,56,49484,93993,211,4594');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable675 values('4,7,1,10,90,23');
Query OK, 1 row affected (0.41 sec)
mysql> insert into DemoTable675 values('90,854,56,89,10');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable675 values('11,22,344,67,89');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable675;

This will produce the following output −

+----------------------------+
| Value                      |
+----------------------------+
| 10,56,49484,93993,211,4594 |
| 4,7,1,10,90,23             |
| 90,854,56,89,10            |
| 11,22,344,67,89            |
+----------------------------+
4 rows in set (0.00 sec)

Following is the query to search within a table of comma-separated values −

mysql> select *from DemoTable675
   WHERE Value LIKE '10,%'
   OR Value LIKE '%,10'
   OR Value LIKE '%,10,%'
   OR Value= '10';

This will produce the following output −

+----------------------------+
| Value                      |
+----------------------------+
| 10,56,49484,93993,211,4594 |
| 4,7,1,10,90,23             |
| 90,854,56,89,10            |
+----------------------------+
3 rows in set (0.00 sec)
raja
Published on 26-Aug-2019 06:48:43
Advertisements