How to find a particular varchar id in MySQL from a list of values?

MySQLMySQLi Database

To get a particular varchar ID from a list, you can use FIND_IN_SET(). Let us first create a table −

mysql> create table DemoTable
(
   Id varchar(255)
);
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10,100,1000');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('101,120,2');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('3,4,5');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| Id          |
+-------------+
| 10,100,1000 |
| 101,120,2   |
| 3,4,5       |
+-------------+
3 rows in set (0.00 sec)

Following is the query to find the id in MySQL −

mysql> select *from DemoTable where find_in_set('2',Id);

This will produce the following output −

+-----------+
| Id        |
+-----------+
| 101,120,2 |
+-----------+
1 row in set (0.03 sec)
raja
Published on 25-Sep-2019 15:52:14
Advertisements