Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?


Yes, you can search for particular numbers using the MySQL FIND_IN_SET(). Let us first create a table −

mysql> create table DemoTable
(
   ListOfNumbers varchar(100)
);
Query OK, 0 rows affected (1.24 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('784,746,894,344');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('456,322,333,456');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('654,785,678,456');
Query OK, 1 row affected (0.47 sec)
mysql> insert into DemoTable values('123,676,847,785');
Query OK, 1 row affected (0.34 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------------+
| ListOfNumbers   |
+-----------------+
| 784,746,894,344 |
| 456,322,333,456 |
| 654,785,678,456 |
| 123,676,847,785 |
+-----------------+
4 rows in set (0.00 sec)

Following is the query to search for particular numbers in a record −

mysql> select *from DemoTable where find_in_set('456',ListOfNumbers);

This will produce the following output −

+-----------------+
| ListOfNumbers   |
+-----------------+
| 456,322,333,456 |
| 654,785,678,456 |
+-----------------+
2 rows in set (0.28 sec)

Updated on: 09-Oct-2019

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements