How can I match a comma separated list against a value in MySQL?

MySQLi DatabaseMySQL

Let us first create a table −

mysql> create table DemoTable
(
   `Values` varchar(50)
);
Query OK, 0 rows affected (1.15 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('23,45,78,56');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('384,476,7456');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('68,8,88,89');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('78,80,84');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| Values       |
+--------------+
| 23,45,78,56  |
| 384,476,7456 |
| 68,8,88,89   |
| 78,80,84     |
+--------------+
4 rows in set (0.00 sec)

Following is the query to match a comma-separated list against a value −

mysql> select *from DemoTable where find_in_set('8',`Values`)!=0;

This will produce the following output −

+------------+
| Values     |
+------------+
| 68,8,88,89 |
+------------+
1 row in set (0.00 sec)
raja
Published on 03-Oct-2019 11:06:37
Advertisements