How to use a comma-separated string in an `IN ()` in MySQL?

MySQLMySQLi Database

Set the comma-separated string in the IN() as in the below syntax:

select *from yourTableName where yourColumnName IN('yourCommaSeparatedValue');

Let us first create a table −

mysql> create table DemoTable1314
-> (
-> Number varchar(100)
-> );
Query OK, 0 rows affected (0.53 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1314 values('45,67,89');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1314 values('10,20,50');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1314 values('90,56,45');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1314;

Output

+----------+
| Number   |
+----------+
| 45,67,89 |
| 10,20,50 |
| 90,56,45 |
+----------+
3 rows in set (0.00 sec)

Following is the query to use a comma-separated string in MySQL `IN ()`:

mysql> select *from DemoTable1314 where Number IN('10,20,50');

Output

+----------+
| Number   |
+----------+
| 10,20,50 |
+----------+
1 row in set (0.02 sec)
raja
Published on 04-Nov-2019 15:16:30
Advertisements