Finding a specific row which has several ids separated by comma in MySQL?


To find a row, use FIND_IN_SET(). Let us first create a table −

mysql> create table DemoTable
   -> (
   -> ListOfIds varchar(200)
   -> );
Query OK, 0 rows affected (0.72 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('100,2093,678,686');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values('0595,9585,4885,95959');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('0059954,95986884,9059596,9005');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-------------------------------+
| ListOfIds                     |
+-------------------------------+
| 100,2093,678,686              |
| 0595,9585,4885,95959          |
| 0059954,95986884,9059596,9005 |
+-------------------------------+
3 rows in set (0.00 sec)

Following is the query to find a specific row which has several ids separated by comma in MySQL −

mysql> select *from DemoTable where FIND_IN_SET('9005', ListOfIds);

Output

+-------------------------------+
| ListOfIds                     |
+-------------------------------+
| 0059954,95986884,9059596,9005 |
+-------------------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements