Tutorialspoint

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


1 Answer
Rama Giri

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)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.